Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello guys.
I have created a server application and clients connect to it. I know that we can enable keep-alive socket option so that the listening socket is not dropped after a timeout. MSDN has an example that sets keep-alive option on listening sockets(server sockets), But my understanding is that the client should send keep-alive messages to server, so it is natural to set keep-alive option in client sockets, not server sockets. I am confused, I don't know if I should set keep-alive option in client sockets, or in server sockets. Currently, I enable keep-alive option in client sockets, and it works in many networks, but In some networks, for example, the local network in the company I work in, it doesnt work and the server socket is dropped after 2 hours. The server ÓS is windows 2003 server, and clients are windows 7. I searched MSDN many times, but I failed to understand it.

Edit: My question simply is this:
Should I set keep-alive option in Client socket, Server socket, or Both?

thanks in advance
mr.abzadeh
Posted
Updated 19-Feb-14 16:34pm
v2
Comments
SoMad 19-Feb-14 22:56pm    
Sorry, I didn't see your update before I posted Solution 2. I would answer that by saying you should set it on both sides, but I don't think it will solve your problem. If it fails on your local network when you have it configured on one end, it will most likely still fail even if you configure it on both ends.

Soren Madsen

The TCP timeout for keepalive defaults to 2 hours. .NET has an API to vary this else you can set in the registry to a max of 1192 hours.

http://blogs.technet.com/b/nettracer/archive/2010/06/03/things-that-you-may-want-to-know-about-tcp-keepalives.aspx[^]


Read this to find out more about keepalive:

http://tools.ietf.org/html/rfc1122#page-101[^]

Quote:
"A TCP keep-alive mechanism should only be invoked in
server applications that might otherwise hang
indefinitely and consume resources unnecessarily if a
client crashes or aborts a connection during a network
failure."
 
Share this answer
 
v3
Comments
mr.abzadeh 19-Feb-14 22:43pm    
My question simply is this:
Should I set keep-alive option in Client socket, Server socket, or Both?
I dont use .Net and I can set timeout in Windows API, But It is steel unclear for me wether I should set TCP timeout in Server side or client side?
[no name] 20-Feb-14 1:18am    
Solution updated.
SoMad 20-Feb-14 1:52am    
Interesting. I did not know about that (referring to the quote you added).

Soren Madsen
This thread[^] is pretty valuable as it verifies, that you are not the only one having problems with this feature. Allow me to quote one of the main points as I believe it is relevant to the problem you are experiencing on your local network:
Quote:
The mechanisms are almost exactly the same as sending your own message, only difference is the packets contains minimum of data - return contains only header, so it might be chopped away (not routed) by some routers/firewalls. In most projects I tend to end up writing a ping mechanism of my own because the other ends need to know a network fault at the same time (ping message not received within a given interval means down). Writing a mechanism of your own can also give you a quicker ways of detecting it or you have to change send timeouts etc to match your specs.
The link in Solution 1 is also excellent - I was going to include it in my answer as well :).


Personally, I turned my back to the built-in keep-alive over 15 years ago. Perhaps I did not have enough experience at the time, perhaps the old versions of Windows did not implement the feature well enough in the TCP stack. In any case, I could not make it work reliably and ever since then, I have implemented my own keep-alive mechanism. I know you didn't really ask about this, but since it sounds like you have control of the code on both the client and the server side, I feel you will be better off implementing your own keep-alive as well.

There are many, many ways of designing this. In the beginning, I would have both sides send keep-alives, the receiver should return an ACK and things like that, but over the years I have found the following approach to be sufficient just about anywhere I use persistent TCP connections.
- Determine which side will be sending data most often. Let's call that side A, the other side is side B. Depending on the nature of your applications, side A can be either the server or the client.
- Determine the maximum time a link can be down before your application should react on it (1 second, 10 seconds, 30 seconds, etc.). Let's call that T. You do not want to flood the network with these messages, so go as high as you can while still being able to react to a link going down as quickly as you need to.
- Define your keep-alive message. It should obviously be pretty small (I tend to have a time stamp in it and nothing else).


While the systems are connected:
- Side A must keep track of the last time it sent a message (data or keep-alive). If more than T/3 seconds have passed since a message was sent, send a keep-alive message. If the link has gone down, the socket send operation will fail and the program has to react to it.
- Side B must keep track of the last time it received a message (data or keep-alive). If more than T seconds have passed since a message was received, consider the link down and react to it.


I feel this is a fairly simple approach and it works great for me.

[EDIT]
Sorry, I didn't see your update before I posted this Solution. I would answer you basic question by saying you should set it on both sides, but I don't think it will solve your problem. If it fails on your local network when you have it configured on one end, it will most likely still fail even if you configure it on both ends.
[/EDIT]

Soren Madsen
 
Share this answer
 
v3
Comments
[no name] 20-Feb-14 1:23am    
I agree with you and our client server apps use a "ping" in this way.
mr.abzadeh 20-Feb-14 2:24am    
Thanks, very usefull. I implemented my own keep-alive by calling my GetVersion in client side, and every thing is ok
SoMad 20-Feb-14 2:40am    
Great! Glad I could help.

Soren Madsen

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900