1

All the tutorials I have read and watched about java socket programming involve one common, essential word, which is localhost. I've been playing with java server-client programms for a while, did a little bit of experimentation. But everytime, my computer was both the server and the client, so I didn't quite understand how to make things work when it comes to other other computers that try to connect to the server hosted on my computer.

Take the following basic server-client classes, for instance:

Server-Class

public class TestServerSocket {

    public static void main(String args[]) throws IOException {
        final int portNumber = 444;
        System.out.println("Creating server socket on port " + portNumber);
        ServerSocket serverSocket = new ServerSocket(portNumber);

        while (true) {
            Socket socket = serverSocket.accept();
            OutputStream os = socket.getOutputStream();
            PrintWriter pw = new PrintWriter(os, true);
            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));

             //...code...
       }
            socket.close();
            pw.close();
            os.close();
            br.close();
    }
}

Client-Class

public class TestClientSocket {

    public static void main(String args[]) throws IOException {
        final String host = "localhost";
        final int portNumber = 444;
        System.out.println("Creating socket to '" + host + "' on port " + portNumber);

        while (true) {
            Socket socket = new Socket(host, portNumber);
            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
            BufferedReader userInputBR = new BufferedReader(new InputStreamReader(System.in));

            //...code...

        }
            socket.close();
            out.close();
            br.close();
            userInputBR.close();
     }
}

Now, say I want to run the TestServerSocket on my computer, and have my friend run the TestClientSocket on his. What changes will there have to be made to this code in order for that to work?

1

1 Answer 1

4

"localhost" is essentially an alias for your loopback IP address 127.0.0.1

For your friend to hook up to your server, the code would have to have your server's IP address in place of "localhost".

Important thing to note, if your friend is not on your local network, you will have to use your outbound IP address and port forward the proper ports on your router. Although, if you are not familiar with this type of networking, I wouldn't attempt it. You can open yourself up for a myriad of network based attacks if you're not careful and even if you are careful.

Sign up to request clarification or add additional context in comments.

1 Comment

Could you give me a few links to serve as documentation for the " if your friend is not on your local network" part, or maybe elaborate a little just for this example?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.