“java.io.IOException:连接超时”和“SocketTimeoutException:读取超时”有什么区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17147780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between "java.io.IOException: Connection timed out" and "SocketTimeoutException: Read timed out"
提问by Ekans
If I set a socket SoTimeout, and read from it. when read time exceed the timeout limit, I'll get an "SocketTimeoutException: Read timed out". and here is the stack in my case:
如果我设置了一个套接字 SoTimeout,并从中读取。当读取时间超过超时限制时,我会收到“SocketTimeoutException: Read timed out”。这是我的情况下的堆栈:
java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:150)
at java.net.SocketInputStream.read(SocketInputStream.java:121)
at java.io.FilterInputStream.read(FilterInputStream.java:133)
at org.apache.hadoop.ipc.Client$Connection$PingInputStream.read(Client.java:277)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)
at java.io.BufferedInputStream.read(BufferedInputStream.java:254)
at java.io.DataInputStream.readInt(DataInputStream.java:387)
at org.apache.hadoop.ipc.Client$Connection.receiveResponse(Client.java:527)
at org.apache.hadoop.ipc.Client$Connection.run(Client.java:462)
but here I encountered "IOExcetion: Connection timed out", i don't know how it happened. Stacks:
但是在这里我遇到了“IOExcetion:连接超时”,我不知道它是怎么发生的。堆栈:
java.io.IOException: Connection timed out
at sun.nio.ch.FileDispatcher.read0(Native Method)
at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:21)
at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:198)
at sun.nio.ch.IOUtil.read(IOUtil.java:171)
at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:245)
at org.apache.hadoop.net.SocketInputStream$Reader.performIO(SocketInputStream.java:55)
at org.apache.hadoop.net.SocketIOWithTimeout.doIO(SocketIOWithTimeout.java:142)
at org.apache.hadoop.net.SocketInputStream.read(SocketInputStream.java:155)
at org.apache.hadoop.net.SocketInputStream.read(SocketInputStream.java:128)
at java.io.FilterInputStream.read(FilterInputStream.java:116)
at org.apache.hadoop.ipc.Client$Connection$PingInputStream.read(Client.java:277)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
at java.io.BufferedInputStream.read(BufferedInputStream.java:237)
at java.io.DataInputStream.readInt(DataInputStream.java:370)
at org.apache.hadoop.ipc.Client$Connection.receiveResponse(Client.java:527)
at org.apache.hadoop.ipc.Client$Connection.run(Client.java:462)
Can someone tell me what's the differences between the two exceptions, Thanks.
有人能告诉我这两个例外有什么区别吗,谢谢。
回答by fge
A connection timeout means you attempted to connect to the remote IP/port pair and failed to do so: it did not answer at all. Another possible error at that stage would be connection refused, in which this pair is available but rejected your connection attempt. Both of these errors appear on the initial setup of a socket. Note that these errors only occur with TCP, since a TCP connection requires the establishment of a session.
连接超时意味着您尝试连接到远程 IP/端口对但未能成功:它根本没有应答。在那个阶段另一个可能的错误是连接被拒绝,其中这对可用但拒绝了您的连接尝试。这两个错误都出现在套接字的初始设置中。请注意,这些错误仅发生在 TCP 上,因为 TCP 连接需要建立会话。
When you have a socket read timeout, it means you areconnected, but failed to read data in time. Timeouts on sockets are configurable. You may also get a connection reset error, which means you did connect successfully, but the other end decided that after all you're not worth it :p
当你有socket读取超时时,说明你已经连接上了,但是没有及时读取数据。套接字超时是可配置的。您也可能会收到连接重置错误,这意味着您确实连接成功,但另一端认为您毕竟不值得:p
回答by Uwe Plonus
Simple answer:
简单回答:
In one case (Connection timed out
) your application cannot connect to the server in a timely manner. In the other case (Read timed out
) the connection can be established but during read the connection times out.
在一种情况下 ( Connection timed out
) 您的应用程序无法及时连接到服务器。在另一种情况下 ( Read timed out
) 可以建立连接,但在读取期间连接超时。
回答by user207421
'Connection timed out' after the connect phase means that something has gone seriously wrong with the connection and it must be closed. 'Read timeout' just means that no data arrived within the specified receive timeout period: it isn't fatal.
连接阶段后的“连接超时”意味着连接出现严重错误,必须关闭。“读取超时”只是意味着在指定的接收超时时间内没有数据到达:它不是致命的。