Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ConnectionTimeout versus SocketTimeout

I'm having a problem with a library that I am using. It might be the library or it might be me using it wrong!

Basically, when I do this (Timeout in milliseconds)

_ignitedHttp.setConnectionTimeout(1);  // v short _ignitedHttp.setSocketTimeout(60000);  // 60 seconds 

No timeout exception is generated and it works ok, however, when I do the following,

_ignitedHttp.setConnectionTimeout(60000);  // 60 seconds _ignitedHttp.setSocketTimeout(1);          // v short 

I get a Socket Exception.

So, my question is why can I not simulate a Connection Exception? Am I misunderstanding the difference between a socket and a connection time-out? The library is here (not officially released yet).

like image 639
Robert Avatar asked Sep 09 '11 10:09

Robert


People also ask

What is connectionTimeout?

Connection timeout is a common error that occurs whenever the client is waiting for too long before getting a response from any server (for API calls or browser requesting pages).

What is the difference between Readtimeout and Connecttimeout?

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

Why do socket timeouts happen?

Socket timeouts can occur when attempting to connect to a remote server, or during communication, especially long-lived ones. They can be caused by any connectivity problem on the network, such as: A network partition preventing the two machines from communicating. The remote machine crashing.


2 Answers

A connection timeout occurs only upon starting the TCP connection. This usually happens if the remote machine does not answer. This means that the server has been shut down, you used the wrong IP/DNS name, wrong port or the network connection to the server is down.

A socket timeout is dedicated to monitor the continuous incoming data flow. If the data flow is interrupted for the specified timeout the connection is regarded as stalled/broken. Of course this only works with connections where data is received all the time.

By setting socket timeout to 1 this would require that every millisecond new data is received (assuming that you read the data block wise and the block is large enough)!

If only the incoming stream stalls for more than a millisecond you are running into a timeout.

like image 190
Robert Avatar answered Sep 19 '22 14:09

Robert


A connection timeout is the maximum amount of time that the program is willing to wait to setup a connection to another process. You aren't getting or posting any application data at this point, just establishing the connection, itself.

A socket timeout is the timeout when waiting for individual packets. It's a common misconception that a socket timeout is the timeout to receive the full response. So if you have a socket timeout of 1 second, and a response comprised of 3 IP packets, where each response packet takes 0.9 seconds to arrive, for a total response time of 2.7 seconds, then there will be no timeout.

like image 37
entpnerd Avatar answered Sep 18 '22 14:09

entpnerd