TCP connection articles

How to keep a long TCP connection?

Use KeepAlive (content of http1.1)

 TCP recognizes different requests?

Every time a connection is established, a unique socket will be saved. With this socket, you will know the other party's IP address, port number and other information. Through this socket, you can send information to the specified party. .

Heartbeat packet mechanism

Why is it called a heartbeat package? This is because it is sent every fixed time like a heartbeat to tell the server that the client is alive, in fact, to keep the connection alive.

In the TCP mechanism, there is a heartbeat packet mechanism, and the system defaults to a 2-hour heartbeat frequency. But it can't detect the disconnection of machine power failure, network cable unplugging, and firewall.

Implementation idea: After the client connects to the server, the server maintains an online user dictionary. The client sends a heartbeat packet to the server at regular intervals. After the server receives the packet, the value of the dictionary data will be updated to 0. If the terminal does not receive the packet from the client within the specified time, the dictionary data will be incremented by one. When the value of the dictionary data is greater than or equal to three, it will be regarded as disconnected.

The underlying principle: use setTimeOut(function, timeOut) to regularly send requests to the server

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325257690&siteId=291194637