0

I created a simple echo server in Java. When I try it locally, it works as it should. However, when I try to connect it from a different computer using the IP address and the port number the server is running on, it never connects. Is there anything else that should be done to connect to a server from a different computer?

import java.net.Socket;
import java.net.ServerSocket;

public class EchoServer {

    public static void main(String[] args) throws Exception {

        // create socket
        int port = 4444;
        ServerSocket serverSocket = new ServerSocket(port);
        System.err.println("Started server on port " + port);

        // repeatedly wait for connections, and process
        while (true) {

            // a "blocking" call which waits until a connection is requested
            Socket clientSocket = serverSocket.accept();
            System.err.println("Accepted connection from client");

            // open up IO streams
            In  in  = new In (clientSocket);
            Out out = new Out(clientSocket);

            // waits for data and reads it in until connection dies
            // readLine() blocks until the server receives a new line from client
            String s;
            while ((s = in.readLine()) != null) {
                out.println(s);
            }

            // close IO streams, then socket
            System.err.println("Closing connection with client");
            out.close();
            in.close();
            clientSocket.close();
        }
    }
}
5
  • 1
    Maybe the firewall is blocking your connection. Try to disable it for a short test. Are you separated from the other computer by a router? Commented Mar 26, 2012 at 20:48
  • 2
    Add code how do you create server socket? Commented Mar 26, 2012 at 20:49
  • Call netstat -na and check if the address you bind the server. It should be 0.0.0.0:* (or :::* for ipv6) Commented Mar 26, 2012 at 20:51
  • 3
    We need to see the server code at least, but in general the two main causes of this are usually a firewall or the server only listening on the local loopback interface. Commented Mar 26, 2012 at 20:51
  • I closed my antivirus and firewall. And in netstat -na I see the port I was listening on. Output looked like this: TCP 0.0.0.0:12555 0.0.0.0:0 LISTENING Commented Mar 26, 2012 at 21:00

2 Answers 2

1

Please check the following things.

  1. Is the server computer behind a network proxy ?

  2. Does it have an independent public IP Address by which it is accessible from anywhere ? Or, does it have an internal IP, by which it can be accessed in your LAN ?

  3. Make sure FireWalls has an exception for port 4444. Or you may turn it of in both client and server.

If it does not help, post the exception you are getting (by editing the question). Or the server program is just freezing without any error ?

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

2 Comments

I am sure firewall is turned off. How can I check the first two?
Open ipchicken.com from both the computers. If both IPs are same, you are behind network proxy and your computers does not have individual public IP.
0

If this is on your LAN refer to the machine running your EchoServer by name (the actual machine name, I believe they show you to do it this way on the Sun Tutorial that posted this echo server excercise correct?). If that works it would help a lot in troubleshooting the issue.

Comments

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.