tags:

views:

255

answers:

2

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

My lib i use can handle connect 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

A: 

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).

Kevin Montrose
ok, thanks for your suggest :)
QuanNH
how to kill the thread in safe way when send/recv timeout?
QuanNH
maybe i create a new question :D
QuanNH
A: 

'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.

bryantsai
QuanNH