In PHP network programming, handling multiplexing is a key technology for achieving efficient network services. socket_set_option and socket_select are two important functions in PHP socket programming. Many developers will wonder whether they can be used together, especially how to match correctly when implementing multiplexing.
This article will explain in detail the role of socket_set_option and socket_select , discuss whether they can be used together, and introduce how to use them to achieve multiplexing.
socket_set_option is used to set options for sockets, such as timeout time, whether to reuse the address, send/receive buffer size, etc. Its function is to adjust the behavioral characteristics of the socket.
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
The above code sets the address reuse option for socket, which is often used to quickly bind ports when server restarts.
socket_select is a key function to implement multiplexing. It is used to listen for multiple sockets, and when one or more of the sockets are ready to read and write, the function returns. This allows a single-threaded program to handle multiple connections simultaneously.
$read = [$socket1, $socket2];
$write = null;
$except = null;
$timeout = 5;
$num_changed = socket_select($read, $write, $except, $timeout);
socket_select returns those prepared sockets by modifying the $read , $write , and $except arrays.
The answer is yes , but the functions of the two are different. When used together, you should clearly distinguish the uses:
socket_set_option is used to configure socket behavior (such as timeouts, buffers, etc.).
socket_select is used to listen to whether multiple sockets are readable, writable or have exceptions.
Multiplexing mainly relies on socket_select , while socket_set_option only adjusts the properties of socket, usually set after socket is created and before socket_select is used.
The following is a simple server example showing how to set socket options with socket_set_option while using socket_select to multiplex multiplexed listening for multiple client connections.
<?php
// create TCP socket
$server = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($server, SOL_SOCKET, SO_REUSEADDR, 1);
// Bind port
socket_bind($server, '0.0.0.0', 8080);
socket_listen($server);
$clients = [];
$read = [$server];
while (true) {
$readSockets = $read;
$write = null;
$except = null;
// Listen to multiple socket Changes
$numChanged = socket_select($readSockets, $write, $except, 5);
if ($numChanged === false) {
echo "socket_select An error occurred\n";
break;
} elseif ($numChanged > 0) {
// Listen to a new client connection
if (in_array($server, $readSockets)) {
$newClient = socket_accept($server);
if ($newClient !== false) {
socket_set_option($newClient, SOL_SOCKET, SO_RCVTIMEO, ["sec"=>5, "usec"=>0]);
$clients[] = $newClient;
$read[] = $newClient;
echo "New client connection\n";
}
unset($readSockets[array_search($server, $readSockets)]);
}
// Process data sent by existing clients
foreach ($readSockets as $socket) {
$data = socket_read($socket, 1024, PHP_NORMAL_READ);
if ($data === false || $data === '') {
// Connection closes
socket_close($socket);
unset($clients[array_search($socket, $clients)]);
unset($read[array_search($socket, $read)]);
echo "Client disconnected\n";
} else {
$data = trim($data);
echo "Received client data: $data\n";
// Send a response
socket_write($socket, "The server has been received: $data\n");
}
}
}
}
socket_close($server);
Key points description:
The server socket $server sets SO_REUSEADDR with socket_set_option to ensure fast port reuse.
After the new client connection is established, the receiving timeout option is also set with socket_set_option .
socket_select listens to all active client sockets and server sockets to ensure that the multiplexing mechanism works properly.
socket_set_option and socket_select functions are different, but they can and should be used in combination.
socket_set_option is responsible for configuring socket parameters and optimizing network performance and behavior.
socket_select is responsible for multiplexing and listening for multiple socket events.
Using these two functions reasonably can write efficient and stable network server programs.
If you want to learn PHP socket multiplexing in depth, you can also refer to official documents and related network programming books to master more details and advanced skills.
The URL domain name for the example in the article is as follows:
$url = "https://gitbox.net/api/socket_demo";