简体   繁体   English

用于公共ip的java客户端服务器程序

[英]java client server program for public ip

I have written a normal program for client server communication in java. 我在java中编写了一个用于客户端服务器通信的正常程序。 It runs well on localhost and on private network. 它在localhost和专用网络上运行良好。 Now I want the program to communicate with remote machine at other place which is connected to internet. 现在我想让程序与连接到互联网的其他地方的远程机器进行通信。 Here is my code. 这是我的代码。

Client code 客户代码

public class GreetingClient
  {
    public static void main(String [] args)
      {
        String serverName = "27.123.66.43";
        int port = Integer.parseInt("5005");
       try
         {
           System.out.println("Connecting to " + serverName +
     " on port " + port);
           Socket client = new Socket(serverName, port);
           System.out.println("Just connected to " 
     + client.getRemoteSocketAddress());
          OutputStream outToServer = client.getOutputStream();
          DataOutputStream out = new DataOutputStream(outToServer);
          out.writeUTF("Hello from "
                  + client.getLocalSocketAddress());
          InputStream inFromServer = client.getInputStream();
          DataInputStream in =
                    new DataInputStream(inFromServer);
          System.out.println("Server says " + in.readUTF());
          client.close();
       }catch(IOException e)
          {
             e.printStackTrace();
          } 
      }
 }

Server Code 服务器代码

import java.net.*;
import java.io.*;

public class GreetingServer extends Thread {
private ServerSocket serverSocket;

public GreetingServer(int port) throws IOException {
    serverSocket = new ServerSocket(port);
    //serverSocket.setSoTimeout(10000);
}

public void run() {
    while (true) {
        try {
            System.out.println("Waiting for client on port "
                    + serverSocket.getLocalPort() + "...");
            Socket server = serverSocket.accept();
            System.out.println("Just connected to "
                    + server.getRemoteSocketAddress());
            DataInputStream in
                    = new DataInputStream(server.getInputStream());
            System.out.println(in.readUTF());
            DataOutputStream out
                    = new DataOutputStream(server.getOutputStream());
            out.writeUTF("Thank you for connecting to "
                    + server.getLocalSocketAddress() + "\nGoodbye!");
            server.close();
        } catch (SocketTimeoutException s) {
            System.out.println("Socket timed out!");
            break;
        } catch (IOException e) {
            e.printStackTrace();
            break;
        }
    }
}

public static void main(String[] args) {
    int port = Integer.parseInt("5005");
    try {
        Thread t = new GreetingServer(port);
        t.start();
    } catch (IOException e) {
        e.printStackTrace();
    }
  }
}

Server has got public ip as 27.123.66.43 . 服务器的公共IP为27.123.66.43 This program does not work for public ip. 该程序不适用于公共IP。 How can I use this program for public ip? 如何将此程序用于公共IP?

Since it works on your local private network, I think you maybe need to check if the port 5005 on your remote server is allowed to access from your client server. 由于它可以在您的本地专用网络上运行,我认为您可能需要检查是否允许远程服务器上的端口5005从您的客户端服务器进行访问。 You can have a test by executing the following commands: 您可以通过执行以下命令进行测试:

# I assume your remote server is a *nix server, here you can use nc to create a TCP server
nc -l 27.123.66.43 5005

# use telnet to connect to your remote server on your client server
telnet 27.123.66.43 5005

If the server is behind a router, then you will have to set up port forwarding. 如果服务器位于路由器后面,则必须设置端口转发。 This is done in the router configuration. 这是在路由器配置中完成的。 Your java code stays the same. 你的java代码保持不变。 In the router configuration, forward TCP on port 5005 to your server which will have a local network IP of 192.168.100.40 for instance. 在路由器配置中,将端口5005上的TCP转发到您的服务器,该服务器的本地网络IP为192.168.100.40。

There is no 'direct connection' to the Internet. 互联网没有“直接连接”。 It must be going through a router somewhere. 它必须通过某个地方的路由器。 Your WiFi router for example. 例如你的WiFi路由器。 So the port must be opened. 所以必须打开端口。 Also, you must have an externally visible IP address provided by your Internet Service Provider (ISP). 此外,您必须具有Internet服务提供商(ISP)提供的外部可见IP地址。 Either a single address or range of addresses. 单个地址或地址范围。 Normally that is only available with a business level service (not your usual home Internet for example). 通常情况下,这仅适用于业务级别的服务(例如,不是通常的家庭Internet)。 In a production environment, servers are given static IP addresses. 在生产环境中,服务器被赋予静态IP地址。 Desktops and laptops are given dynamic IPs which can change from time to time, and the outside world has no way of knowing that. 桌面和笔记本电脑被赋予动态IP,这些IP可能会不时发生变化,外界无法知道这一点。 So make sure your port (5005) is open to the outside. 因此,请确保您的端口(5005)对外开放。 Best practice for router setup is not to allow ANY incoming ports, for security reasons. 出于安全原因,路由器设置的最佳做法是不允许任何传入端口。 Then only open those specifically needed (HTTP, HTTPS, FTP, whatever). 然后只打开那些特别需要的(HTTP,HTTPS,FTP,等等)。

Oh, and watch out for systems with multiple Internet Ports. 哦,请注意具有多个Internet端口的系统。 Each Port will usually have a different IP address! 每个端口通常都有不同的IP地址!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM