"java.net.ConnectException: Connection timed out" 和 "java.net.SocketTimeoutException: connect timed out" 之间的确切区别是什么?

What is the exact difference between "java.net.ConnectException: Connection timed out" and "java.net.SocketTimeoutException: connect timed out"?

在我的日志中,我可以找到以下两个堆栈跟踪

Caused by: java.net.ConnectException: Connection timed out  
at java.net.PlainSocketImpl.socketConnect(Native Method)    
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345) 
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)  
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)   
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)   
at java.net.Socket.connect(Socket.java:589) 
at sun.net.NetworkClient.doConnect(NetworkClient.java:175)  
at sun.net.www.http.HttpClient.openServer(HttpClient.java:432)  
at sun.net.www.http.HttpClient.openServer(HttpClient.java:527)  
at sun.net.www.http.HttpClient.<init>(HttpClient.java:211)  
at sun.net.www.http.HttpClient.New(HttpClient.java:308) 
at sun.net.www.http.HttpClient.New(HttpClient.java:326) 
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1168)    
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1147)   
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:998) 
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:932)  
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(HttpURLConnection.java:1282)    
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1257) 
at com.sun.xml.internal.ws.transport.http.client.HttpClientTransport.getOutput(HttpClientTransport.java:104)    ... 127 more

Caused by: java.net.SocketTimeoutException: connect timed out   
at java.net.PlainSocketImpl.socketConnect(Native Method)    
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345) 
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)  
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)   
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)   
at java.net.Socket.connect(Socket.java:589) 
at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:656)   
at sun.net.NetworkClient.doConnect(NetworkClient.java:175)  
at sun.net.www.http.HttpClient.openServer(HttpClient.java:432)  
at sun.net.www.http.HttpClient.openServer(HttpClient.java:527)  
at sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:275)  
at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:371) 
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191)  
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1147)   
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:998) 
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177)   
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(HttpURLConnection.java:1282)    
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1257) 
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:250)   
at com.sun.xml.internal.ws.transport.http.client.HttpClientTransport.getOutput(HttpClientTransport.java:104)    ... 50 more

两个异常都发生在使用 JAXWS-RI(参考实现)调用 Web 服务期间。这两个异常之间的确切区别是什么。

为了配置超时,我在上下文中设置了一些属性,一个用于连接超时:

BindingProvider bindingProvider = (BindingProvider) port;
Map<String, Object> context = bindingProvider.getRequestContext();
context.put(JAXWSProperties.REQUEST_TIMEOUT, 90000);
context.put(JAXWSProperties.CONNECT_TIMEOUT, 90000);

据我从日志中看到的,只有 java.net.SocketTimeoutException 似乎符合 JAXWSProperties.CONNECT_TIMEOUT 设置。

java.net.ConnectExceptions 发生在 90 秒结束之前。

它们在网络传输层方面相当低级,并且在 TCP/IP 状态机的不同状态下被错误抛出。如果有兴趣,您可以研究 TCP 套接字图,但大多数网络应用程序(JAX-WS 是)用户或程序员不需要关心这些底层细节,除了网络不工作的事实。

它们来自不同的代码路径,一个是建立 http 连接,另一个是 https。 https是基于SSL,即secure socket layer,试图连接不同参数的socket。

最重要的是,这些代码路径是由其他人编写的,可能是古代程序员。当时他们决定抛出一个异常而不是另一个。我们不必为此大惊小怪。

我想我的问题已经得到解答 here

提到的两个异常来自本机代码。由于 user5436 already mentioned, the SocketTimeoutExceptions occurs when a timeout is set and no connection can be established in the desired time. The ConnectException 被抛出,当尝试建立连接时发生错误。令人困惑的是,这也可能是 连接超时

我怀疑其中一个来自 connect(..., timeout) 路径而另一个来自未指定连接超时的路径,但没有证据。在前一种情况下,我希望 SocketTimeoutException,在后一种情况下,我希望 ConnectException.