为什么有在异常差异在Windows平台上
在Windows平台上的套接字连接,运行此代码:为什么有在异常差异在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
;
我很困惑,为什么异常是不同的,这个异常导致我正在使用这个框架的一些问题(春天丝带,相关的问题是:ribbon issue)
希望任何人都可以解释:
为什么例外是不同
我什么时候会得到“SocketTimeoutException:合作无限超时“,我什么时候能得到另一个?
韩国社交协会很多
@Jonathan罗塞恩
我reverve的代码,并得到了相同的:
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)
回答:
因为您指定了不同的连接超时。对于实际的连接超时,500ms太短,但是,由于您指定了连接超时,并且在该超时时间内连接没有成功,所得到的是连接超时,这只是预期的。当你指定一个更现实的(仅仅是)1100毫秒的超时时间时,你会得到潜在的问题,这是一个拒绝连接,因为没有监听目标IP:端口。
注意你在这个代码中有一个套接字泄漏。
以上是 为什么有在异常差异在Windows平台上 的全部内容, 来源链接: utcz.com/qa/264962.html