In PHP, the control of blocking and non-blocking modes is very important when handling network communications. socket_set_block and stream_set_blocking are two commonly used functions. They are both used to set the blocking behavior of sockets or streams, but there are some differences in the applicable objects and usages. This article will introduce the differences between these two functions and their usage methods in detail.
Blocking Mode : Function calls will wait for the operation to be completed, such as reading data until data is readable, writing data until transmission is completed, and the program will pause and wait during this time.
Non-blocking Mode : The function call will not wait for the operation to complete. If the operation cannot be completed immediately, it will return immediately and the program can continue to execute other tasks.
Applicable to: socket resources in PHP Socket extension (created through functions such as socket_create() )
Function: Set the socket to blocking mode.
Function prototype:
bool socket_set_block(resource $socket)
Return value: Return true for success, return false for failure
Suitable for: stream-based resources , such as the resource returned by fopen() , stream_socket_client() , or the stream resource converted by socket_create() through socket_import_stream() .
Function: Set the blocking mode of the flow.
Function prototype:
bool stream_set_blocking(resource $stream, int $mode)
parameter:
$stream : Streaming resources
$mode : 1 means blocking, 0 means non-blocking
Return value: Return true for success, return false for failure
characteristic | socket_set_block | stream_set_blocking |
---|---|---|
Applicable resources | Socket resources in Socket extensions | PHP streaming resources (file streams, network streams, etc.) |
Blocking mode setting method | Call directly without additional parameters | Specify the blocking mode with the second parameter (0 or 1) |
Usage flexibility | Only blocking can be set, socket_set_nonblock() is required for non-blocking | Blocking or non-blocking can be set |
Applicable scenarios | Low-level socket programming | High-level network flow programming |
<?php
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
die("socket_create failed: " . socket_strerror(socket_last_error()));
}
socket_set_block($socket); // Set to blocking mode
// Connect to the server
socket_connect($socket, "gitbox.net", 80);
// Send a request
$request = "GET / HTTP/1.1\r\nHost: gitbox.net\r\nConnection: Close\r\n\r\n";
socket_write($socket, $request, strlen($request));
// Read the response
$response = '';
while ($out = socket_read($socket, 2048)) {
$response .= $out;
}
echo $response;
socket_close($socket);
?>
<?php
$stream = stream_socket_client("tcp://gitbox.net:80", $errno, $errstr, 30);
if (!$stream) {
die("Connection failed: $errstr ($errno)");
}
// Set to blocking mode
stream_set_blocking($stream, 1);
// Send a request
fwrite($stream, "GET / HTTP/1.1\r\nHost: gitbox.net\r\nConnection: Close\r\n\r\n");
// Read the response
$response = '';
while (!feof($stream)) {
$response .= fread($stream, 2048);
}
echo $response;
fclose($stream);
?>
socket_set_block is specially used for socket resources in PHP's Socket extension, and directly sets the socket to a blocking state.
stream_set_blocking is suitable for a wider range of stream resources and provides flexible switching between blocking and non-blocking modes.
Which function to choose depends mainly on the type of resource you use: if you use Socket extension to manage sockets, use socket_set_block ; if you use stream resources (such as stream_socket_client ), use stream_set_blocking .
Understanding these two functions and their differences will help you better control data read and write behavior in PHP network programming and optimize program performance and response.