162

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

2 Answers 2

275

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.

13
  • 1
    Could you ever get a connection timeout if the server is not down but is to busy? Or would that be a socket timeout?
    – Robert
    Sep 9, 2011 at 12:21
  • 10
    That depends - if the TCP connection has been established before the server is overloaded you will get a socket exception - otherwise you will get a connection exception, indicating that the TCP connection could not be established.
    – Robert
    Sep 9, 2011 at 12:25
  • 2
    Considering the high latency of especially older mobile networks the connection timeout has to be set to several seconds (e.g. 10s or better 10000 msec). The socket timeout I would only set if you don't use several connections because HTTP can re-use the connection after a request.
    – Robert
    Sep 9, 2011 at 12:42
  • 1
    Does this mean that if you set a socket timeout (e.g. 1min), then the connection would be killed after 1 mins of inactivity, where as it would normally get re-used if no timeout was set?
    – Robert
    Sep 9, 2011 at 12:56
  • 2
    @Robert You won't necessarily get a connect exception if the server is too busy. It's platform-dependent, on the server platform. A socket read timeout does not kill the connection. It just causes a SocketTimeoutException. Whether the connection is still usable is a decision the application has to make. There's certainly nothing about the API that says you can't try more I/O on the socket. Your statement about not using timeouts if you use multiple connections doesn't begin to make sense. Too much misinformation here.
    – user207421
    Jul 7, 2014 at 7:43
125

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.

4
  • 4
    Okay. 1. So can we say that SocketTimeout only comes into picture when a Connection is already established? 2. What if there is no data flow for like say 5 mins after the 3 packets have been received? Will there be a SocketTimeout exception after the 3rd packet has been received? Jul 20, 2017 at 14:08
  • 4
    @SaurabhPatil 1. Yes. See Wikipedia's Technical Overview of the HTTP protocol for confirmation. 2. Once the end of message is sent, no further data is needed, so a socket timeout won't occur. See this answer on the topic.
    – entpnerd
    Jul 25, 2017 at 3:22
  • 20
    I wish they had named "socket timeout" as "idle timeout". Apr 23, 2018 at 7:18
  • 1
    If you have a socket timeout of 1 second, and a response comprised of 3 IP packets, where each response packet takes 0.8 seconds to arrive, and between the 1st and 2nd packet, there's a gap of 0.3 seconds. Then the total response time is still 2.7 seconds, but there will be a socket timeout.
    – Anderson
    Aug 18, 2020 at 9:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.