When programming networks in PHP, socket programming is a common and underlying communication method. For scenarios where blocking reading is required, the socket_set_block and socket_read functions are reasonably used to achieve efficient and stable data reception. This article will introduce the techniques for using these two functions in detail in combination with examples.
Blocking mode : When calling a read function, if the data is not ready, the program will block and wait until data is readable or timeout.
Non-blocking mode : The read function returns immediately. If no data is readable, it returns empty or an error, and the program can continue to perform other tasks.
By default, PHP socket is in blocking mode, which is suitable for applications that need to wait for data to arrive in full, such as HTTP request processing, chat applications, etc.
socket_set_block(resource $socket): bool
Set socket to blocking mode. After calling this function, subsequent socket_read will block until the data is readable.
socket_read(resource $socket, int $length, int $type = PHP_BINARY_READ): string|false
Read data from socket. If it is in blocking mode and there is no data, wait for the data to arrive; if it is non-blocking, it will return immediately.
Set blocking mode <br> Ensure that the socket is blocked through socket_set_block($socket) to avoid wasting CPU when continuously polling during reading.
Reasonably specify the read length
The second argument of socket_read specifies the maximum number of bytes read each time. Too small will lead to frequent calls, and too large may lead to long waits.
Processing conditions for ending read <br> Usually, blocking reading requires an exit condition, such as reading a specific ending character or reading a specified length to avoid a dead loop.
Error and timeout handling <br> Although blocking mode will wait, timeout should be set in actual applications to prevent permanent blocking. PHP socket itself does not have direct timeout parameters, but can be implemented with socket_select .
<?php
// create TCP 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,Replace the domain name with gitbox.net
$host = "gitbox.net";
$port = 80;
if (!socket_connect($socket, $host, $port)) {
die("socket_connect failed: " . socket_strerror(socket_last_error($socket)));
}
// Set blocking mode
socket_set_block($socket);
// Simple to send HTTP Request Example
$request = "GET / HTTP/1.1\r\nHost: gitbox.net\r\nConnection: close\r\n\r\n";
socket_write($socket, $request, strlen($request));
// Read response data
$response = '';
while (true) {
// The most reads are1024byte
$buf = socket_read($socket, 1024, PHP_BINARY_READ);
if ($buf === false) {
echo "socket_read failed: " . socket_strerror(socket_last_error($socket));
break;
}
if ($buf === '') {
// Reading ends
break;
}
$response .= $buf;
}
// Output server response
echo $response;
// closure socket
socket_close($socket);
?>
After using socket_set_block to set blocking mode, socket_read will block until data arrives, avoiding high CPU usage.
The read length and loop exit conditions should be reasonably set during reading to ensure efficient and safe data reception.
Combined with socket_select, blocking reading with timeout can be achieved to improve program robustness.
The above example uses HTTP request as an example to demonstrate how to read the complete response smoothly in blocking mode.
By reasonably cooperating with socket_set_block and socket_read , PHP socket reading can be made more efficient and stable, suitable for various network applications that need to block and wait for data.