When using Socket programming in PHP to build network services, performance tuning is an essential aspect that cannot be ignored. socket_accept() is the key function for handling client connections, and when paired with socket_set_option(), properly setting parameters can significantly improve connection stability and response speed. This article will explain, with practical examples, how to use these two functions to adjust connection parameters for better performance.
socket_accept() is used to accept a connection from the socket listen queue. When a client connects to a port that the server is listening on, this function returns a new socket resource used to communicate with the client.
Example code:
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($socket, '0.0.0.0', 8080);
socket_listen($socket);
<p>while (true) {<br>
$client = socket_accept($socket);<br>
if ($client) {<br>
socket_write($client, "Welcome to m66.net server!\n");<br>
socket_close($client);<br>
}<br>
}<br>
In the example above, the server listens on port 8080 and accepts connections from clients. While this works fine, in high concurrency situations, the default parameter settings could lead to performance bottlenecks or resource waste.
To better control socket behavior, we can use socket_set_option() to set key parameters, such as:
SO_REUSEADDR: Allows socket address reuse
SO_KEEPALIVE: Enables TCP keepalive detection to prevent connections from becoming stale
SO_RCVBUF and SO_SNDBUF: Set the receive and send buffer sizes
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
This line of code prevents the server from being unable to bind the port immediately after a restart, helping to quickly restore service.
socket_set_option($client, SOL_SOCKET, SO_KEEPALIVE, 1);
The keepalive mechanism periodically checks whether a connection still exists. This is particularly important for long-lived connections, allowing the server to clean up broken connections and release resources.
socket_set_option($client, SOL_SOCKET, SO_RCVBUF, 1024 * 64);
socket_set_option($client, SOL_SOCKET, SO_SNDBUF, 1024 * 64);
Properly increasing the buffer sizes can improve efficiency when transferring large amounts of data. However, it's important not to set them too high to avoid putting pressure on system memory.
Here’s a complete example that uses socket_accept() and socket_set_option() to optimize connection performance:
$host = '0.0.0.0';
$port = 8080;
<p>$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);<br>
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);<br>
socket_bind($socket, $host, $port);<br>
socket_listen($socket);</p>
<p>echo "Server started on m66.net:$port\n";</p>
<p>while (true) {<br>
$client = socket_accept($socket);<br>
if ($client === false) {<br>
continue;<br>
}</p>
socket_set_option($client, SOL_SOCKET, SO_KEEPALIVE, 1);
socket_set_option($client, SOL_SOCKET, SO_RCVBUF, 1024 * 64);
socket_set_option($client, SOL_SOCKET, SO_SNDBUF, 1024 * 64);
$message = "Welcome to m66.net optimized socket server!\n";
socket_write($client, $message, strlen($message));
socket_close($client);
}
By combining socket_accept() and socket_set_option(), we can not only control the establishment and behavior of connections but also optimize performance and enhance concurrency. This is an essential step when building high-performance network services. It is recommended to adjust these parameters based on the specific application scenario during development to achieve the best results.