Click to See Complete Forum and Search --> : How to release dead sockets?


The_Diamond_Z
May 24th, 2004, 04:16 AM
I've written a socket server using IO Completion ports and I'm impressed with the results. So much so that I'm shootting for perfection in this code and I've got one last problem to deal with.

If a client dies without closing the socket connection or calling WSACleanup (I haven't worked out which but I don't think it matters) the connection on the server side remains open and the result is a memory leak (the OVERLAPPED & CompletionKey).

I've set the socket option SO_KEEPALIVE which I expected would cause a socket event when the client dies but no such luck. What else can I do?

As a practical matter, this only happens when someone is running the client code in a debugger and then stops debugging before the end of the process, but such things result in bad PR.

Mathew Joy
May 24th, 2004, 05:29 AM
I guess you are talking about a client going away without telling you (such as power failure, network disconnection). Currently there is no implimented ways in TCP to find it. You have to implement a keep-alive scheme. You send a message and if the client doesn't reply back within a time interval, you assume that the connection is dead and close the socket and release all its associated memory.

kuphryn
May 24th, 2004, 08:30 PM
One solution is to keep track of how long a client socket has is in an inactive or idle state. Close it!

Kuphryn

VipulPathak
May 25th, 2004, 10:20 AM
Hi,

I was in the same problem a couple of days ago. I have the Following Solution for my problem, My Server runs on Windows 2000 Server.


DWORD dwError = 0L ;
tcp_keepalive sKA_Settings = {0}, sReturned = {0} ;
sKA_Settings.onoff = 1 ;
sKA_Settings.keepalivetime = 5500 ; // Keep Alive in 5.5 sec.
sKA_Settings.keepaliveinterval = 3000 ; // Resend if No-Reply
if (WSAIoctl(skNewConnection, SIO_KEEPALIVE_VALS, &sKA_Settings,
sizeof(sKA_Settings), &sReturned, sizeof(sReturned), &dwBytes,
NULL, NULL) != 0)
{
dwError = WSAGetLastError() ;
}


Although, People suggest that Implementing a Self Ping type Protocol, would be better. I didn't implemented such a strategy, coz. I can't change the Communication Protocol.

Hope, this will help.

The_Diamond_Z
May 25th, 2004, 12:37 PM
I love the Internet! Thanks to all that provided suggestions. I've implimented VipulPathak's suggestion and it's spot on.

The only hiccup I had was I needed to include MSTCPiP.h for the definition of tcp_keepalive and SIO_KEEPALIVE_VALS.

I was trying to the same thing with setsockopt, but my sockets are overlapped (IO Completion Ports) and I guess setsockopt is ignored or something.

Thanks again...