简体   繁体   中英

java client server program for public ip

I have written a normal program for client server communication in java. It runs well on localhost and on private network. 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 . This program does not work for public ip. How can I use this program for public 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. 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. 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.

There is no 'direct connection' to the Internet. It must be going through a router somewhere. Your WiFi router for example. So the port must be opened. Also, you must have an externally visible IP address provided by your Internet Service Provider (ISP). 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). In a production environment, servers are given static IP addresses. Desktops and laptops are given dynamic IPs which can change from time to time, and the outside world has no way of knowing that. So make sure your port (5005) is open to the outside. 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).

Oh, and watch out for systems with multiple Internet Ports. Each Port will usually have a different IP address!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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