This should be very easy to implement as part of your health check. If you do not ignore the problem of dropped connections, you probably have a keep-alive system that periodically sends a client-> server message and vice versa if there was no communication. It should be trivial to add the simple โlast received timeโ value to the socket state, and then close the socket if it is too far from DateTime.Now .
But the more important question is: โWhy?โ. The best solution depends on what your reasons for this are in the first place. Do you want to use the server for more clients, dropping those that do not send data? Most likely, you will make things worse, since timeouts for TCP sockets are more like 2-4 minutes, so when you disconnect the client after the 20s and reconnect, it will now use two ports on the server side, rather than one. Unfortunately.
As for your comment on the remote answer, and connection without data send and receive i think it gonna waste your threads points closer to your real problem - the volume of your server connection should not have any relation to how many threads the server uses to service these connections. Thus, the only thing that an open connection will โwasteโ is basically a bit of memory (depending on the amount of memory needed for each connection, plus the cost of the socket with its buffers) and the TCP port. This may be a problem in some applications, but if you ever get to that level of download, you can probably congratulate yourself already. Most likely, you will encounter other resources before approaching the boundaries of the port (an assumption based on what it looks like you are making an MMO game). If you really run into problems with them, you probably want to abandon TCP altogether and rewrite everything in UDP (or, preferably, some kind of ready-made solution on top of UDP).
Luaan source share