1

I have a Java server and client, which is a simple server that prints a message when the client communicates with it. It works perfectly fine when the client and server are on the same machine. When I place the client on a different machine I get the following exception:

java.net.ConnectException: Connection refused
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:579)
    at java.net.Socket.connect(Socket.java:528)
    at java.net.Socket.<init>(Socket.java:425)
    at java.net.Socket.<init>(Socket.java:208)
    at DateClient.main(DateClient.java:16)

I tried running the server on a different machine(which is not the client), and it works fine. So, there is something about the machine that won't allow the client to connect to the port. I tried pinging the server from the client and it's able to reach the server. I switched off the firewall using sudo /etc/init.d/iptables stop but the client still can't connect to the server. Any help would be appreciated!

1

"Connection Refused" means: The TCP connection does reach the server (or is being explicitly blocked by a firewall between the client and the server), assuming the latter isn't true - there are 3 possiblities: 1. The IP the client is connecting to isn't the IP or interface the server process is listening on, thus the operating system rejects the connection. 2. The server program is explicitly rejecting the connection because it internally is filtering the request 3. There's another firewall process on the server that is rejecting the request.

First, verify the server process is listening on a valid IP/PORT:

sudo netstat -apn | grep "server process name"

You should get a response like (for an example "server process name" of smbd - Samba):

user@host:~# sudo netstat -apn | grep smbd
tcp        0      0 192.168.10.1:139        0.0.0.0:*               LISTEN      7721/smbd

Note the "192.168.10.1:139" - you're going to want to see the specific IP that your client is connecting to, or 0.0.0.0:139 (which means all interfaces, all local IPs). If you see 127.0.0.1 or an IP which is not reachable from the client then your problem is there.

Second, verify your server program doesn't internally block incoming connections from certain hosts (since you've given no info on what the server is, I'll have to leave that up to you)

Lastly, if you're certain the server process is listening on a valid interface that's reachable by the client, and the server program isn't filtering incoming connections, then go back to the basics:

1- Validate the request is hitting the server, where eth0 and the tcp port would be changing to suit your environment:

sudo tcpdump -i eth0 -n -v tcp port 139

You'll see 2-way traffic:

user@host:~# sudo tcpdump -i eth1 -n -v tcp port 139
tcpdump: listening on eth1, link-type EN10MB (Ethernet), capture size 65535 bytes
12:27:45.339982 IP (tos 0x0, ttl 128, id 12153, offset 0, flags [DF], proto TCP (6), length 52)
  192.168.10.10.24411 > 192.168.10.1.139: Flags [S], (output cut)

Note the 192.168.10.10.24411 (The client) talks to ">" 192.168.10.1.139 (The server) Flags [S] with a TCP SYN (-> [S]) packet

The next packet shown by TCP dump will likely have flags of [R.] (Reset - connection refused)

So, from that, if you see the SYN then RST - Server is definitely blocking the connection, perhaps run your server program in debug mode to show what's going on.

If you don't see that SYN packet (and you've double checked the tcp port and interface you specified for tcpdump) then you've got a firewall or maybe a routing issue between the client and the server.

Good luck.

1

This exception usually occurs when there is no service listening on the port you are trying to connect to. Any of these things could be happening:

  1. You have not started your server.
  2. Your server is not waiting to accept connections.
  3. You are trying to connect to the wrong port number.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.