2

I am going to create a socket and get an InputStream. Here is how I try it.

try {
    final String serverIP = "111.111.111.111";
    final int serverPort = Integer.parseInt(server_port);
    final InetAddress serverAd=InetAddress.getByName(serverIP);
    final InetAddress localAd =InetAddress.getByName(local_ip);  
    final int localPort = 4040;

    Socket socket = new Socket(serverAd, serverPort, localAd, localPort);  
}  

But there is an exception thrown,

java.net.ConnectException: Connection refused: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
    at java.net.Socket.connect(Socket.java:529)
    at java.net.Socket.connect(Socket.java:478)
    at java.net.Socket.<init>(Socket.java:375)
    at java.net.Socket.<init>(Socket.java:276)
    at shootist.Porter.run(Porter.java:41)

Here the server sends me rtp data and server side is ok and confirmed. I sent invite and got 200 as well. If there is a problem in my IP and port, I think, all responses cannot delivered to my IP and given Ports. But it can't happen as the server sends me responses to my IP and given port number. How I can fix this issue? Where I am wrong and what?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Débora
  • 5,816
  • 28
  • 99
  • 171
  • Add firewall exception to the port number on the server and the client machine. Even better, throw your firewall out of your PC. – Acn Jan 11 '12 at 07:27
  • Thanks. Let me know how to add a a firewall Exception to the portnumber on the client machine. – Débora Jan 11 '12 at 08:05

2 Answers2

11

A "connection refused" error means the socket stack on the server machine received your connection request and intentionally refused to accept it. That happens for one of two possible reasons:

1) there is no listening socket running on the port you are trying to connect to.

2) there is a listening socket, but its backlog of pending connections is full, so there is no room to queue your request at that moment.

To differentiate between the two, try reconnecting a few times with a delay in between each attempt. If you get the same error consistently, then #1 is likely the culprit. Make sure the port number is correct. If #2 is the culprit, your reconnect has a chance of succeeding eventually.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Thank you Remy.I changed port numbers and tried. but same error :(. Would you let me know how to create a successful listening socket.(I m lettle bit new.) – Débora Jan 11 '12 at 08:10
  • Would you explain more "but its backlog of pending connections is full, so there is no room to queue.... ". Any way when I sent the INVITE and then server response with 200 and server starts to sending me data.I have to wait till the server sends the response and get the corresponding server's port(because server's port is defined in the response.INVITE is sent to another port and socket is to be created on another port which the server decides.) As you mentioned, I tried to reconnect with the socket. but it failed as the same error occurred. – Débora Jan 11 '12 at 08:37
  • 1
    When a client attempts to connect to a server, the request is placed into the server's queue until it calls `accept()` to dequeue the request. The queue has a finite size, which is specified when the socket starts listening. Look at the documentation for the `backlog` parameter of the constructor for the `java.net.ServerSocket` class. – Remy Lebeau Jan 12 '12 at 00:00
  • If you cannot connect to the IP/Port that the server returns to your INVITE, then make sure the IP is accessible from your client machine, and that there is not a firewall/router in between that may be blocking the connection. – Remy Lebeau Jan 12 '12 at 00:03
  • I stored some Ip address in a text file, then when I try to send a message to them, this error shows for me. because of some IP addresses are not reachable. what's your idea to check if there is no listening socket running? I tried [this answer](https://stackoverflow.com/a/7685486/7858689) , but it's not working for me. – hassan moradnezhad Jul 28 '19 at 16:22
  • @hassanmoradnezhad the only reliable way is to attempt to connect to them and handle any errors that may be thrown – Remy Lebeau Jul 28 '19 at 18:28
1

Connection refused means your are try to connect to a server which is not listening on that port, or is too backlogged to accept the connection.

A simple way to test this is to try

 telnet 111.111.111.111 4040
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 1
    that is not the only condition that can cause the error. see my answer. – Remy Lebeau Jan 11 '12 at 07:33
  • @RemyLebeau-TeamB Good point on the backlog. The server would have to be in a pretty bad way for it to be back logged. – Peter Lawrey Jan 11 '12 at 07:49
  • Not necessarily. The backlog might be a little too small and the server's `accept()` rate might be coded to run a little too slow. You never really know. – Remy Lebeau Jan 12 '12 at 00:05
  • A common pattern is to have an acceptor thread which does nothing but accept and create handlers. However, a loaded server might deliberately slow down accepting. – Peter Lawrey Jan 12 '12 at 06:59