开发者

Handling network timeouts in Java

i have a java program connect to server through xot protocol.

My lib i use can handle connec开发者_如何学Pythont timeout, but there is no method like setSoTimeout() to handle timeout when send & recv data.

so, anyone could suggest me some solution for this problem.

thanks

Quan


One option is to spawn a thread to do the writing and join(timeout) it. Likewise with reading from the connection. Obviously kill the thread (and treat the connection as in an indeterminate state) when the timeout expires (as opposed to the thread dieing).


'Socket.setSoTimeout()' should apply to recv as well. See its javadoc.

public void setSoTimeout(int timeout) throws SocketException

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. The option must be enabled prior to entering the blocking operation to have effect. The timeout must be > 0. A timeout of zero is interpreted as an infinite timeout.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜