Java 套接字的连接超时和读取超时有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3069382/
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 is the difference between connection and read timeout for sockets?
提问by corgrath
3 questions:
3个问题:
What is the difference between connectionand readtimeout for sockets?
What does connectiontimeout set to "infinity" mean? In what situation can it remain in an infinitive loop? and what can trigger that the infinity-loop dies?
What does readtimeout set to "infinity" mean? In what situation can it remain in an infinitive loop? and what can trigger that the infinity-loop dies?
套接字的连接超时和读取超时有什么区别?
什么连接超时设置为“无限”是什么意思?在什么情况下它可以保持在不定式循环中?什么可以触发无限循环死亡?
什么读超时设置为“无限”是什么意思?在什么情况下它可以保持在不定式循环中?什么可以触发无限循环死亡?
采纳答案by Stephen C
1) What is the difference between connection and read timeout for sockets?
1) 套接字的连接超时和读取超时有什么区别?
The connection timeout is the timeout in making the initial connection; i.e. completing the TCP connection handshake. The read timeout is the timeout on waiting to read data1. Specifically, if the server fails to send a byte <timeout> seconds after the last byte, a read timeout error will be raised.
连接超时是建立初始连接的超时时间;即完成 TCP 连接握手。读取超时是等待读取数据的超时时间1。具体来说,如果服务器在最后一个字节后发送一个字节 <timeout> 秒失败,则会引发读取超时错误。
2) What does connection timeout set to "infinity" mean? In what situation can it remain in an infinitive loop? and what can trigger that the infinity-loop dies?
2)连接超时设置为“无穷大”是什么意思?在什么情况下它可以保持在不定式循环中?什么可以触发无限循环死亡?
It means that the connection attempt can potentially block for ever. There is no infinite loop, but the attempt to connect can be unblocked by another thread closing the socket. (A Thread.interrupt()
call may also do the trick ... not sure.)
这意味着连接尝试可能会永远阻塞。没有无限循环,但是连接的尝试可以被关闭套接字的另一个线程解除阻塞。(Thread.interrupt()
电话也可以解决问题……不确定。)
3) What does read timeout set to "infinity" mean? In what situation can it remain in an infinite loop? What can trigger that the infinite loop to end?
3) 读取超时设置为“无穷大”是什么意思?什么情况下可以无限循环?什么可以触发无限循环结束?
It means that a call to read
on the socket stream may block for ever. Once again there is no infinite loop, but the read
can be unblocked by a Thread.interrupt()
call, closing the socket, and (of course) the other end sending data or closing the connection.
这意味着read
对套接字流的调用可能会永远阻塞。再一次没有无限循环,但是read
可以通过Thread.interrupt()
调用、关闭套接字和(当然)另一端发送数据或关闭连接来解除阻塞。
1 - It is not ... as one commenter thought ... the timeout on how long a socket can be open, or idle.
1 - 它不是......正如一位评论者所想的......套接字可以打开或空闲多长时间的超时。
回答by ZZ Coder
These are timeout values enforced by JVM for TCP connection establishment and waiting on reading data from socket.
这些是 JVM 为建立 TCP 连接和等待从套接字读取数据而强制执行的超时值。
If the value is set to infinity, you will not wait forever. It simply means JVM doesn't have timeout and OS will be responsible for all the timeouts. However, the timeouts on OS may be really long. On some slow network, I've seen timeouts as long as 6 minutes.
如果该值设置为无穷大,您将不会永远等待。这只是意味着 JVM 没有超时,操作系统将负责所有超时。但是,操作系统上的超时可能真的很长。在某些慢速网络上,我见过长达 6 分钟的超时。
Even if you set the timeout value for socket, it may not work if the timeout happens in the native code. We can reproduce the problem on Linux by connecting to a host blocked by firewall or unplugging the cable on switch.
即使您为套接字设置了超时值,如果超时发生在本机代码中,它也可能不起作用。我们可以通过连接到被防火墙阻止的主机或拔掉交换机上的电缆来在 Linux 上重现该问题。
The only safe approach to handle TCP timeout is to run the connection code in a different thread and interrupt the thread when it takes too long.
处理 TCP 超时的唯一安全方法是在不同的线程中运行连接代码,并在时间过长时中断该线程。