java套接字超时行为 [英] java socket timeout behaviour
问题描述
如果我在 Java 套接字上设置 soTimeout,那么在主动对等方与被动对等方的情况下会发生什么行为.例如,如果我有一个 1 分钟的 readtimeout 值并且有一个文件传输并且需要 5 分钟,它是否会出现 readtimeout 异常?对我来说,有必要在连接挂起时获得超时异常.
If I set soTimeout on java sockets what will be the behaviour in case of active peer vs passive peer. For instance if I have a readtimeout value 1 minute and having a file transfer and which takes 5 minutes will it get readtimeout exception or not ? For me its necessary to get timeout exception when connection hangs.
推荐答案
soTimeout
设置显式影响从套接字输入流读取的操作.您可以将其视为允许调用者在读取操作上定义定时块.来自 setSoTimeout
的 Javadoc:
The soTimeout
setting explicitly affects operations that read from the socket's input stream. You can think of it as allowing the caller to define a timed block on read operations. From the Javadoc for setSoTimeout
:
使用指定的超时时间(以毫秒为单位)启用/禁用 SO_TIMEOUT.将此选项设置为非零超时后,对与此 Socket 关联的 InputStream 将仅为此阻塞时间.如果超时到期,则java.net.SocketTimeoutException 被引发,尽管 Socket 仍然是有效.
Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds. With this option set to a non-zero timeout, a read() call on the InputStream associated with this Socket will block for only this amount of time. If the timeout expires, a java.net.SocketTimeoutException is raised, though the Socket is still valid.
在被动对等体的情况下,不会仅仅因为对等体没有调用read
而引发超时.但是,如果它确实进行了读取调用,则该调用必须在 soTimeout
到期之前返回数据,否则将引发 SocketTimeoutException.
In the case of a passive peer, no timeout will be thrown solely due to the peer not calling read
. However, if and when it does make a read call, the call must return data before the soTimeout
expires, or else a SocketTimeoutException will be raised.
这篇关于java套接字超时行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!