javasocketsexceptionsocket-timeout-exceptionconnectexception

Why there are differences in the exception In socket connection on Windows platform


On Windows platform, I run this code:

code:

Socket socket = new Socket();
    try {
        socket.connect(new InetSocketAddress("127.0.0.1", 8080), 500);
    } catch (Exception e) {
        e.printStackTrace();
    }
    socket = new Socket();
    socket.connect(new InetSocketAddress("127.0.0.1", 8080), 1100);

127.0.0.1:8080 is not accessible, but I got different exception:

java.net.SocketTimeoutException: connect timed out
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at com.hash.test.TestRxJava.main(TestRxJava.java:14)
Exception in thread "main" java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at    java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at com.hash.test.TestRxJava.main(TestRxJava.java:19)

As you can see, one is SocketTimeoutException and the other is ConnectException;

I am confused why the Exception is different, and this exception causes some problem in the framework I am using ( Spring Ribbon, associated issue is: ribbon issue)

Hope anyone can explain:

  1. Why the Exception is different

  2. When will I get "SocketTimeoutException: connect timed out" and when will I get the other?

Tks a lot


@Jonathan Rosenne

I reverve the code and got the same:

Socket socket = new Socket();
    try {
        socket.connect(new InetSocketAddress("127.0.0.1", 8080), 1100);
    } catch (Exception e) {
        e.printStackTrace();
    }
    socket = new Socket();
    socket.connect(new InetSocketAddress("127.0.0.1", 8080), 500);

Result:

java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at com.transsnet.afbet.factsCenter.sdk.SDKTest.main(SDKTest.java:726)
Exception in thread "main" java.net.SocketTimeoutException: connect timed out
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at com.transsnet.afbet.factsCenter.sdk.SDKTest.main(SDKTest.java:731)

Solution

  • Because you specified different connect timeouts. 500ms is far too short for a realistic connect timeout, but, since you specified it, and the connect didn't succeed within that timeout period, what you got was a connect timeout, which is only to be expected. When you specified a more realistic (only just) timeout of 1100ms, you got the underlying problem, which was a connection refusal, because nothing is listening at the target IP:port.

    NB You have a socket leak in this code.