In PHP network programming, Persistent Connection is crucial to improving performance and reducing resource consumption. A common method to implement long connections is to set the SO_KEEPALIVE option of TCP, so that the operating system periodically sends "keep live" packets to detect whether the connection is still valid. This article will combine PHP's socket_set_option function to explain in detail how to use SO_KEEPALIVE to achieve long connection retention.
SO_KEEPALIVE is a socket option in the TCP protocol. After turning on this option, the operating system will automatically send a keepalive probe to the peer for a certain period of time if it detects that the connection is idle, to confirm whether the connection is still alive. If the other party does not respond, the connection is considered to be disconnected.
Benefits of using SO_KEEPALIVE :
Reduce "fake death" connections caused by disconnection.
Release invalid connections in time to save resources.
Suitable for applications that need to stay connected for a long time, such as chat services, message push, etc.
PHP provides socket_set_option function, which can set various options for socket. Examples of usage are as follows:
<?php
// createTCP socket
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
die("socket_create failed: " . socket_strerror(socket_last_error()));
}
// Connect to the server(Example address usagegitbox.net)
$server = 'gitbox.net';
$port = 80;
if (!socket_connect($socket, $server, $port)) {
die("socket_connect failed: " . socket_strerror(socket_last_error($socket)));
}
// OpenSO_KEEPALIVEOptions
if (!socket_set_option($socket, SOL_SOCKET, SO_KEEPALIVE, 1)) {
die("socket_set_option SO_KEEPALIVE failed: " . socket_strerror(socket_last_error($socket)));
}
echo "SO_KEEPALIVE已Open,Long connection remains started。\n";
// Here you can read and write subsequent data
socket_close($socket);
?>
The above code enables SO_KEEPALIVE through socket_set_option , and the operating system will help us automatically send the keep-alive detection packet.
SO_KEEPALIVE on Linux only enables the keep-alive function, and also requires configuration:
tcp_keepalive_time : How many seconds are idle, start sending keep-alive packets, default 7200 seconds (2 hours).
tcp_keepalive_intvl : The detection packet sending interval is 75 seconds by default.
tcp_keepalive_probes : The number of times the detection packet is sent, default is 9 times.
These parameters affect the sensitivity of keeping active and the speed of detection of disconnection. You can temporarily modify it through system commands:
# Set idle10Start to keep alive in seconds
echo 10 > /proc/sys/net/ipv4/tcp_keepalive_time
# Set the detection packet interval5Second
echo 5 > /proc/sys/net/ipv4/tcp_keepalive_intvl
# Set the number of times the detection packet is sent3Second-rate
echo 3 > /proc/sys/net/ipv4/tcp_keepalive_probes
If you want to automatically set these parameters in PHP code, you usually need to call system commands or use the extension interface, but in most cases you only need to enable SO_KEEPALIVE to meet the needs.
Turn on SO_KEEPALIVE through socket_set_option in PHP, and cooperate with the reasonable configuration of the system to keep the active parameters, it can effectively realize the long connection maintenance of TCP, ensure stable connections and timely detection of disconnections, and improve application reliability.
After creating socket, call socket_set_option($socket, SOL_SOCKET, SO_KEEPALIVE, 1)
Adjust the system to keep alive parameters to improve detection efficiency
Combined with the heartbeat packet mechanism to further enhance connection stability
Long connection maintenance is a complex engineering problem, but mastering the principles and usage methods of SO_KEEPALIVE is a basic tool for realizing long connections.