

It's the internet, and no one expects it to be magic! :)Įxpanding on comments by mbargiel and mycelo on the accepted answer, the following can be used with a non-blocking socket on the server end to inform whether the client has shut down. But it's no so big a deal to have a little delay in updating the server, as long as you DO update. Maybe even a combination could be implemented in some sort of options list (enum flags or something) if you're really worried about it. You could even use a background thread to achieve this with precise timing.

But if I did choose the latter, I'd simply have the server "ping" each client every so often with a single byte, and see if we have a timeout or no response. You can use the suggested methods here, or implement a "heartbeat", as also suggested. I've even seen it occur in online games you're buddy says "goodbye", and he appears to be online for another 1-2 minutes until the server "cleans house".
#RAN ONLINE PRIVATE SERVER DISCONNECT AFTER 20 MINS PROFESSIONAL#
You will see the same behavior exhibited even in professional applications utilizing this protocol (and even others). It's a fact of life I've come to realize. "That's just the way TCP works and you have to live with it." This way, I'm able to safely detect disconnection between TCP client and server. SocketPacket theSockId = (SocketPacket)asyn.AsyncState public void OnDataReceived(IAsyncResult asyn) SocketFlags.None, new AsyncCallback(OnDataReceived), packet) Īnd in callback, here is caught timeout SocketException, which raises when socket doesn't get ACK signal after keep-alive packet. I'm also using asynchronous reading: socket.BeginReceive(packet.dataBuffer, 0, 128, Socket.IOControl(IOControlCode.KeepAliveValues, inOptionValues, null) void SetKeepAlive(bool on, uint keepAliveTime, uint keepAliveInterval)īitConverter.GetBytes((uint)(on ? 1 : 0)).CopyTo(inOptionValues, 0) īitConverter.GetBytes((uint)keepAliveTime).CopyTo(inOptionValues, size) īitConverter.GetBytes((uint)keepAliveInterval).CopyTo(inOptionValues, size * 2) The keepAliveInterval parameter specifies the interval, in milliseconds, between when successive keep-alive packets are sent if no acknowledgement is received. The keepAliveTime parameter specifies the timeout, in milliseconds, with no activity until the first keep-alive packet is sent. I'm using it this way: after the socket is connected, I'm calling this function, which sets keepAlive on. Someone mentioned keepAlive capability of TCP Socket.
