Question

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.

On Windows: An existing connection was forcibly closed by the remote host

On Linux: Connection timed out

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.

Can I found a soultion to this problem or should I simply try to reconnect to the server when the connection is forcibly closed?

EDIT: 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 Section 2.4) but I applied two different approach to solve this problem:

  1. Use TCP/IP keep alive using setsockopt (Section 4.2)
  2. Make an application level keep alive. This is more reliable since the first approach is OS related.
Était-ce utile?

La solution

It depends on what your application is supposed to do. A little more information and perhaps the code you use for listening and handling connections could be of help.

Regardless, technically a longer keep alive time, should prevent the OS from cutting you off. So perhaps it is something else causing the trouble.

Such a thing could be router malfunction or traffic causing your keep-alive packet to get lost. If you aren't already testing it on a LAN (without heavy trafic) I suggest doing so.

It might also be due to how your socket is handled (which I can't determine from your question) This article might help. Non blocking socket with timeout

I'm not used to how connections are handled on Linux, but I expect the OS won't cut off a connection unnecessary. You can re-establish connection as a recovery, but you need to take into account that not all disconnects are gentle, and therefore you could end up making recovery on a connection you actually wish to be closed.

Since it is TCP, it will do its best to make a gentle disconnect, but you can send a custom message telling the server or client not to re-establish the connection right before disconnecting. That way you be absolutely sure, despite that it should be unnecessary to do so.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top