Disconnect the connector after a certain time when data is not received

I simply disconnect my connectors from the server, which do not send any data after a certain time, for example 20 seconds.

I wonder if it works well with timers or something special for this in the socket library? Working with timers on the server for each socket makes it difficult.

Is it unsafe to do a client program? For example, each client disconnects after it does not send data for a while.

+5
source share
3 answers

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).

+2
source

The Client-Server model describes how a client should connect to a server and fulfill requests. What I would recommend to you is to connect to the server, and when you finish receiving all the necessary dates, close the socket (on the client side).

The server will eventually find the freed socket resources, but you can check the Connected property to free the resources sooner.

0
source

When a client disconnects from the server, the server may receive a disconnect event. it looks like

 socket.on('disconnect', function () { // Disconnect event handling }); 

On the client side, you will also find the disconnect event .. in which you need to reconnect the server.

0
source

Source: https://habr.com/ru/post/1210753/


All Articles