I currently have an asynchronous TCP/IP socket C# WinForms application that is the “Server” application. The application holds persistent sockets to the clients. I would like to implement a heartbeat packet to detect Half-Open connections.
Initially I thought about just creating a loop and sending the heartbeat to my entire collection of sockets every xx number of seconds. However, I think this would be bad for performance.
I think I should somehow stagger the heartbeat and send to a few hundred sockets at once. Does anyone have any experience/techniques/implementations for effectively accomplishing this?
I do this (for an order-of-magnitude more sockets) simply by tracking how long ago I last spoke to (or heard from) a particular socket.on an interval, I ping those over a given age. Assuming sockets connect / disconnect / communicate uniformly, this gives me a fairly uniform distribution. Besides, you can always cap the ping to a finite number of sockets per iteration: because you’re tracking the last-talked info, the ones you don’t get to will still be eligible next time.