title: TCP/IP long-term connections
body: <p>I have a server application which runs on a Linux machine. I can connect this application from Windows/Linux machines and can send/recieve data. After a few hours, something occurs and I get following error on the client side.</p>
  
  <p>On Windows: <code>An existing connection was forcibly closed by the remote host</code></p>
  
  <p>On Linux: <code>Connection timed out</code></p>
  
  <p>I have made a search on the web and found some posts which suggest to increase/decrease OS's keep alive time. However, it didin't work for me.</p>
  
  <p>Can I found a soultion to this problem or should I simply try to reconnect to the server when the connection is forcibly closed?</p>
  
  <p><strong>EDIT:</strong> I have tracked the situation. I sent a data to the remote node and sent another data after waiting 5 hours. Sending side sent the first data, but whet the sender sent the second data it didn't response. TCP/IP stack of the sender repeated this 5 times by incrementing the times between retries. Finally, sender reset the connection. I can't be sure why this is happening (Maybe because of a firewall or NAT - see <a href="http://www.tldp.org/HOWTO/html_single/TCP-Keepalive-HOWTO/" rel="nofollow">Section 2.4</a>) but I applied two different approach to solve this problem:</p>
  
  <ol>
  <li>Use TCP/IP keep alive using setsockopt (<a href="http://www.tldp.org/HOWTO/html_single/TCP-Keepalive-HOWTO/" rel="nofollow">Section 4.2</a>)</li>
  <li>Make an application level keep alive. This is more reliable since the first approach is OS related.</li>
  </ol>