为什么 Windows 平台上的套接字连接异常存在差异

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

在Windows平台上,我运行这段代码:

代码:

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 无法访问,但我得到了不同的异常:

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)

如你所见,一个是SocketTimeoutException,另一个是ConnectException

我很困惑为什么异常不同,这个异常导致我正在使用的框架出现一些问题(Spring 功能区,相关问题是ribbon issue)

希望有人能解释一下:

  1. 为什么异常不同

  2. 我什么时候能得到"SocketTimeoutException: connect timed out",我什么时候能得到另一个?

非常感谢


@Jonathan Rosenne

我翻了一下代码,结果一样:

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);

结果:

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)

因为您指定了不同的连接超时。 500 毫秒对于实际的连接超时来说太短了,但是,由于您指定了它,并且在该超时期限内连接没有成功,所以您得到的是连接超时,这是可以预料的。当您指定一个更实际的(只是)超时 1100 毫秒时,您遇到了潜在的问题,即连接拒绝,因为目标 IP:port.

没有任何监听

注意您在此代码中存在套接字泄漏。