169

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

Bilesh Ganguly
  • 3,792
  • 3
  • 36
  • 58
Robert
  • 37,670
  • 37
  • 171
  • 213

2 Answers2

285

A connection timeout occurs only upon starting the TCP connection. This usually happens if the remote machine does not answer. If you get an ConnectException, possible reasons are: 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 time the connection is regarded as stalled/broken. Of course this only works with connections where data is received all the time and there are no delays longer than the configured socket timeout.

By setting socket timeout to 1000 this would require that every second 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 second you are running into a timeout.

This is especially important when HTTP servers process a complex request that requires some time on server side before the HTTP response data is available. If you configure socket timeout to 10000 (10 seconds) but the HTTP server requires 15 seconds after receiving the HTTP request, then you will never get the response as after 10 seconds you will get an SocketTimeoutException (no data is transmitted between reception of the HTTP request until the HTTP response is ready).

Robert
  • 39,162
  • 17
  • 99
  • 152
  • 2
    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 09 '11 at 12:21
  • 11
    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 09 '11 at 12:25
  • Also what are typical values of the socket timeout and connection timeout for mobile devices? Is one usually bigger than the other? – Robert Sep 09 '11 at 12:29
  • 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 09 '11 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 09 '11 at 12:56
  • Android sets both values to 60 seconds: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.3.4_r1/android/net/http/AndroidHttpClient.java#AndroidHttpClient – mxk Sep 09 '11 at 13:01
  • 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 07 '14 at 07:43
  • @EJP: Interesting detail about the socket timeout. But regarding the "multiple connections" I can not follow you. I never talked about that topic in my answer. – Robert Jul 07 '14 at 11:10
  • I wish they had named "socket timeout" as "idle timeout". – Manish Maheshwari Apr 23 '18 at 07:18
  • @Robert Do connection timeouts and socket timeouts result in the same HTTP status code ie. 408 ? – sainiankit Jul 31 '19 at 17:06
  • @sainiankit If you get an HTTP status code this code has been sent from the server. In case of a connection timeout the connection was never established - hence you can't get any status code. In case of a socket timeout the status code may have been sent already, however the socket timeout is an error that occurs on client side, therefore no status code related to this error will occur. – Robert Jul 31 '19 at 17:23
  • @Robert I'm really confused here. What exactly results in 408 ? – sainiankit Aug 01 '19 at 19:43
  • @sainiankit May be you just read the documentation? E.g. here: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/408 – Robert Aug 02 '19 at 12:55
131

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.

entpnerd
  • 10,049
  • 8
  • 47
  • 68
  • 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? – Saurabh Patil Jul 20 '17 at 14:08
  • 4
    @SaurabhPatil 1. Yes. See [Wikipedia's Technical Overview of the HTTP protocol](https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Technical_overview) 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](https://stackoverflow.com/a/4824738/4851565) on the topic. – entpnerd Jul 25 '17 at 03:22
  • 23
    I wish they had named "socket timeout" as "idle timeout". – Manish Maheshwari Apr 23 '18 at 07: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 '20 at 09:03
  • This explains it in a much better way than the accepted answer – rahul rachh May 16 '23 at 19:45