Search code examples
javasocketsclient-serverserversocket

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?


Solution

  • 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