java.net.ConnectException: Connection refused
Solution 1:
This exception means that there is no service listening on the IP/port you are trying to connect to:
- You are trying to connect to the wrong IP/Host or port.
- You have not started your server.
- Your server is not listening for connections.
- On Windows servers, the listen backlog queue is full.
Solution 2:
I would check:
- Host name and port you're trying to connect to
- The server side has managed to start listening correctly
- There's no firewall blocking the connection
The simplest starting point is probably to try to connect manually from the client machine using telnet or Putty. If that succeeds, then the problem is in your client code. If it doesn't, you need to work out why it hasn't. Wireshark may help you on this front.
Solution 3:
You have to connect your client socket to the remote ServerSocket. Instead of
Socket clientSocket = new Socket("localhost", 5000);
do
Socket clientSocket = new Socket(serverName, 5000);
The client must connect to serverName which should match the name or IP of the box on which your ServerSocket
was instantiated (the name must be reachable from the client machine). BTW: It's not the name that is important, it's all about IP addresses...