When building efficient network services, latency is an important indicator that cannot be avoided. As a general scripting language, PHP also provides rich APIs in network programming, where the socket_accept() function is often used to handle connection reception on the server side. Although it is used relatively simple, if you want to build a low-latency and high-concurrency service, you still need to master a series of optimization techniques and practical application methods.
socket_accept() is a function provided by the PHP Socket extension to accept connection requests from listening sockets. Once the server creates and binds the socket through socket_create() and socket_bind() , socket_listen() starts listening, and then it can receive the client's connection through socket_accept() .
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($socket, '0.0.0.0', 8080);
socket_listen($socket);
while (true) {
$client = socket_accept($socket);
if ($client) {
socket_write($client, "Welcome to visit m66.net Network Services\n");
socket_close($client);
}
}
The above code builds a basic TCP server, but its performance is far from meeting the "low latency" requirements.
Delay caused by blocking I/O <br> By default, socket_accept() is a blocking call, meaning that when there is no client connection, the script will stop at that line, wasting CPU time, and block subsequent logical processing.
Serial processing connection <br> Only one client request is processed at a time, which lacks concurrency capabilities, resulting in queues and thus increases latency.
Improper resource management <br> Failure to release the connection in time, not setting the timeout, and not properly setting the buffer may all affect the response speed.
You can set non-blocking mode through socket_set_nonblock() , or you can use socket_select() to listen to multiple sockets at the same time to avoid blocking and waiting.
socket_set_nonblock($socket);
$clients = [$socket];
while (true) {
$read = $clients;
if (socket_select($read, $write = null, $except = null, 0, 200000)) {
foreach ($read as $sock) {
if ($sock === $socket) {
$client = socket_accept($socket);
if ($client !== false) {
socket_set_nonblock($client);
$clients[] = $client;
socket_write($client, "Welcome to visit m66.net!\n");
}
} else {
$data = socket_read($sock, 1024, PHP_NORMAL_READ);
if ($data === false || $data === "") {
$index = array_search($sock, $clients);
socket_close($sock);
unset($clients[$index]);
} else {
socket_write($sock, "You sent it: $data");
}
}
}
}
}
The timeout time of reception and transmission can be controlled through socket_set_option() to avoid blocking due to connection problems:
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, ["sec"=>1, "usec"=>0]);
socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, ["sec"=>1, "usec"=>0]);
For high-concurrency applications that need to carry thousands of connections, it is recommended to use extensions such as libevent or Swoole . They use more efficient epoll/kqueue models at the bottom, which greatly reduces I/O latency.
// use Swoole 实现高性能Network Services
$server = new Swoole\Server("0.0.0.0", 9501);
$server->on("connect", function ($server, $fd) {
$server->send($fd, "Welcome to visit m66.net Network Services\n");
});
$server->on("receive", function ($server, $fd, $reactor_id, $data) {
$server->send($fd, "You sent it:$data");
});
$server->on("close", function ($server, $fd) {
echo "Client {$fd} Closed\n";
});
$server->start();
Real-time data acquisition service : Receive real-time data from IoT devices through non-blocking sockets.
Online Chat Room : Use Swoole to build high concurrent chat services, and all data exchanges maintain low latency.
Game server backend : Small battle games can use native sockets to achieve low-latency player communication.
API Gateway : Build an intermediate-layer socket service to receive requests and respond quickly to cache data.
Although socket_accept() is the basic function in PHP, to build a truly low-latency service, it is necessary to optimize from multiple dimensions, such as using non-blocking I/O, reasonable resource control, concurrency models, etc. Depending on different business scenarios, choosing appropriate technical solutions (native socket, select, Swoole, Libevent) is the key to achieving high-performance network services. With the help of the above skills, even in the PHP environment, it is possible to create a server-side architecture that is responsive and stable, providing solid technical support for the business.