Keepalive is not a common TCP connection. When we connect to a client as a server, if keeplive is set to true, when the other party does not send any data, if the time exceeds one (depending on the system kernel parameter configuration), an ACK detection packet will be sent to the other party to check whether the TCP/IP connection between the two parties is valid (the other party may break the network ). If this parameter is not set, when the client goes down, the server will never know that the client is down and will still save the invalid connection.
You can also use this parameter on the client. The client socket sends a packet to the server every two hours. This packet does not have any other function, just to check whether the server is still active. If the server does not respond to this packet, the client socket will send another packet after about 11 minutes. If the server does not respond within 12 minutes, the client socket will be closed. If you disable the socket option, the client socket may not be closed for a long time if the server is invalid.
Although keepalive has few benefits, many developers advocate controlling timeout settings and dead sockets in higher-level application code. At the same time, remember that keepalive does not allow you to specify a value for the end point of the probe socket (endpoint. Therefore, it is recommended that developers use anotherA better solution is to modify the socket option of timeout settings..
To put it bluntly: this parameter is actually useless for applications at the application layer. You can use the application layer to understand the status of the server or client and decide whether to continue to maintain the socket.