3

在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

希望任何人都可以解釋:

  1. 爲什麼例外是不同

  2. 我什麼時候會得到「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) 
+0

你可以嘗試顛倒連接嘗試的順序嗎? –

+0

讓我試試扭轉@Jonathan –

回答

1

因爲您指定了不同的連接超時。對於實際的連接超時,500ms太短,但是,由於您指定了連接超時,並且在該超時時間內連接沒有成功,所得到的是連接超時,這只是預期的。當你指定一個更現實的(僅僅是)1100毫秒的超時時間時,你會得到潛在的問題,這是一個拒絕連接,因爲沒有監聽目標IP:端口。

注意你在這個代碼中有一個套接字泄漏。

+0

是的。謝謝。看起來1000毫秒是門檻 –