2

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.

1
  • What do you mean by 'passive peer'? 'Passive' in TCP is used to refer to the listening socket, which doesn't have reads or read timeouts at all. Commented Mar 12, 2013 at 22:28

1 Answer 1

3

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:

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.

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.

Sign up to request clarification or add additional context in comments.

3 Comments

thank you. Just to make it clear, if I set readtimeout 1 minute and call read() on socket, this method will throw exception even if server continues to writing on the other side. is that right ?
@cacert - well, not necessarily. The timeout is based on how long a blocking read on the client side has been waiting. So if your server is writing data but for some reason it doesn't show up in the client buffer before soTimeout is up, then yea, an exception will be raised. But as long as client reads are returning some data before the timeout an exception will not be raised.
ok, this is acceptable. then I think we can say that timeout somehow related with inactive sessions, if we have a long but active session(we regularly retrieve data each time before timeout duration) then read timeout will not be problem. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.