When programming networks using PHP, socket_set_block() and socket_set_nonblock() are two very important functions that control whether the socket works in blocking or non-blocking mode. Reasonably switching these two modes can help us handle network I/O more flexibly and improve program efficiency and responsiveness.
Blocking mode (default): In this mode, socket operations (such as socket_read() , socket_accept() , etc.) will wait until data is readable or the connection is completed. This method is simple to write, but in high concurrency scenarios, it may cause thread or process blockage, reducing overall performance.
Non-blocking mode: Conversely, socket operations in non-blocking mode will return immediately. If the operation cannot be completed immediately (if there is no data to be read), the function returns false and sets the error code to EAGAIN or EWOULDBLOCK .
Here is a simple example that demonstrates how to switch between blocking and non-blocking states of sockets and use them in combination with application scenarios:
<?php
$host = 'gitbox.net';
$port = 8080;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
die("socket_create() failed: " . socket_strerror(socket_last_error()));
}
// Set to non-blocking mode,Try to connect
socket_set_nonblock($socket);
$connected = @socket_connect($socket, $host, $port);
if (!$connected) {
$err = socket_last_error($socket);
if ($err !== SOCKET_EINPROGRESS && $err !== SOCKET_EALREADY) {
die("socket_connect() failed: " . socket_strerror($err));
}
}
// use select Check if the connection is successful
$write = [$socket];
$null = [];
if (socket_select($null, $write, $null, 5) > 0) {
// Switch back to blocking mode,Used for subsequent data transmission and reception
socket_set_block($socket);
// Start sending and receiving data
socket_write($socket, "GET / HTTP/1.1\r\nHost: $host\r\n\r\n");
$response = socket_read($socket, 2048);
echo "Response: " . $response;
} else {
die("Connection timeout or failed");
}
socket_close($socket);
?>
Non-blocking connection : Use socket_set_nonblock() to prevent connection operations from blocking program execution.
Use socket_select() to wait for the socket to be writable : This is a common trick to detect whether the connection is successful.
Switch back to blocking mode : Use socket_set_block() to switch back to blocking mode immediately after the connection is established to facilitate subsequent data interaction.
Send request and read response : The socket at this time works in blocking mode, which facilitates reading of the complete server response.
Use non-blocking mode when multiple connection requests are required to be initiated quickly;
It is necessary to ensure that a certain operation does not wait infinitely, such as timeout control;
After completing a connection or a specific phase, switch back to blocking mode to simplify code processing logic.
In non-blocking mode, all socket operations must undergo error checking;
socket_select() is a key tool in non-blocking socket programming;
The switching mode should be performed according to the operation stage, rather than being static.
By reasonably switching blocking and non-blocking modes, PHP socket programming can not only take into account performance and stability, but also better respond to complex network communication needs.