title: Where does the socket timeout of 21000 ms come from?
body: <p><strong>The Problem</strong></p>
  
  <p>An app I'm maintaining keeps getting socket timeouts after approximately 21000 ms, despite the fact that I've explicitly set longer timeouts.  This seemingly magical value of 21000 ms has come up in a few other SO questions and answers, and I'm trying to figure out exactly where it comes from.</p>
  
  <p>Here's the essence of my code:</p>
  
  <pre><code>HttpURLConnection connection = null;
  try {
  URL url = new URL(urlString);
      connection = (HttpURLConnection) url.openConnection();
      connection.setConnectTimeout(45000);
      connection.setReadTimeout(90000);
      int responseCode = connection.getResponseCode();
      if (responseCode == 200) {
          // code omitted
      }       
  } catch (Exception e) {
      // code omitted
  }
  finally {
      if (connection != null) {
          connection.disconnect();
      }
  }
  </code></pre>
  
  <p>Catching all exceptions in one block is admittedly not ideal, but it's inherited code and I'm reluctant to mess with it.  I know it's catching <code>SocketTimeoutException</code> after 21000 ms because it logs the simple name of the exception class.</p>
  
  <p><strong>Clues</strong></p>
  
  <p>I found a question where an asker was getting a <a href="https://stackoverflow.com/questions/14026223/java-httpurlconnection-timeout-does-not-work"><code>ConnectTimeout</code> after 21000 ms</a>, despite explicitly setting it to 40000 ms.  That's intriguing despite the exception class being different.</p>
  
  <p>I also found a poorly-explained answer which claims that <a href="https://stackoverflow.com/a/10494982/1953590">the server side is responsible for the 21000 ms timeout</a>.</p>
  
  <p><strong>My Hunch</strong></p>
  
  <p>I don't think any action or inaction of the server could cause a shorter-than-expected socket timeout on the client.  But maybe the TCP stacks in Windows and Android share a common ancestor, or at least use similar connect retry logic.</p>
  
  <p>Could it be that Android imposes a maximum connect timeout of 21000 ms, and setting a longer timeout in <code>HttpURLConnection</code> is futile?  Or could this timeout be triggered by some Windows machine on the path between the mobile device and the server?  Do some Android versions throw a <code>SocketTimeoutException</code> where others throw a <code>ConnectException</code>?</p>