3

在 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,相关问题是ribbon issue

希望有人能解释一下:

  1. 为什么异常不同

  2. 我什么时候会得到“SocketTimeoutException:连接超时”,什么时候会得到另一个?

很多


@乔纳森罗森

我恢复了代码并得到了相同的结果:

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

1 回答 1

1

因为您指定了不同的连接超时。500 毫秒对于实际的连接超时来说太短了,但是,由于您指定了它,并且在该超时期限内连接没有成功,所以您得到的是连接超时,这是意料之中的。当您指定一个更现实的(仅)1100 毫秒的超时时,您会遇到潜在的问题,即连接拒绝,因为目标 IP:port 上没有任何东西在侦听。

注意你在这段代码中有一个套接字泄漏。

于 2017-12-27T12:31:56.253 回答