views:

51

answers:

2

3 questions:

1) What is the difference between connection and read timeout for sockets?

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?

3) What does read timeout set to "infinity" mean? In what situation can it remain in an infinitive loop? and what can trigger that the infinity-loop dies?

+3  A: 

1) What is the difference between connection and read timeout for sockets?

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

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?

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

3) What does read timeout set to "infinity" mean? In what situation can it remain in an infinitive loop? and what can trigger that the infinity-loop dies?

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.

Stephen C
"The read timeout is the timeout on waiting to read data.". You wrote "WAITING" to read the data? Read timeout isn't the time how long the socket can be open?
corgrath
In Java you can set "infinity" to the connection and read timeout. I am wondering how these situations can happen (where it will be in an infinitive loop). If you say connection is for TCP handshake, how can the wait be infinitive? Aren't all packets in TCP acked?
corgrath
"Read timeout isn't the time how long the socket can be open?" That is correct. "Aren't all packets in TCP acked?" The timeout says how long to wait for the other end to send a SYN-ACK in response to the initial SYN packet(s).
Stephen C
+1  A: 

These are timeout values enforced by JVM for TCP connection establishment and waiting on reading data from socket.

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.

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.

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.

ZZ Coder