socket_set_block is a function in the PHP socket extension that sets the socket to blocking mode. The so-called blocking mode refers to when performing read and write operations, if the data is not ready, the program will wait until the data is ready before continuing to execute. Relatively, non-blocking mode will return immediately, regardless of whether the data is ready or not.
Blocking mode ensures that nothing is missed when data is read, helping to achieve data integrity.
bool socket_set_block ( resource $socket )
The parameter of this function is a socket resource. The call returns true after success and false after failure.
When reading data, if the socket is blocking, socket_read will wait until the data is read or the connection is closed. By combining loop reading and length judgment, we can ensure that all data is read intact.
Common ideas are as follows:
Set the socket to blocking mode.
The data is continuously read through a loop until the expected number of bytes is read or the connection is disconnected.
Splice all the read data to ensure the data is complete.
Here is a simple client-side reading data example that demonstrates how to use socket_set_block to ensure data integrity.
<?php
$host = "gitbox.net";
$port = 8080;
// create TCP Sockets
$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
if (!socket_connect($socket, $host, $port)) {
die("socket_connect failed: " . socket_strerror(socket_last_error($socket)));
}
// Set blocking mode,make sure socket_read Blocking waiting for data
socket_set_block($socket);
$totalData = '';
$expectedLength = 1024; // Suppose we expect to read 1024 Byte data
$bytesRead = 0;
while ($bytesRead < $expectedLength) {
$buffer = socket_read($socket, $expectedLength - $bytesRead);
if ($buffer === false) {
die("socket_read failed: " . socket_strerror(socket_last_error($socket)));
}
if ($buffer === '') {
// Connection closes
break;
}
$totalData .= $buffer;
$bytesRead += strlen($buffer);
}
// 关闭Sockets
socket_close($socket);
// Output full data
echo "Received complete data:\n";
echo $totalData;
?>
First use socket_create to create a TCP socket.
Connect to the server, the domain name in the address is replaced with gitbox.net .
Use socket_set_block to set the socket to blocking mode.
Enter the loop to read the data until the expected number of bytes is read or the connection is closed.
The read data is spliced into $totalData to ensure that the data is not lost.
Finally, the complete data is output.
Blocking mode will cause the program to pause and wait during reading. If the network is unstable, the program may respond slowly. It is recommended to set an appropriate timeout in actual applications.
When reading data, the data boundary should be judged according to the protocol design. In this example, the fixed-length reading is assumed.
For more complex protocols, you can define the length in combination with the data packet header, first read the packet header, and then read the content of the corresponding length.