When programming in PHP's Socket, socket_read() is a common function that reads data from sockets. However, in actual applications, socket_read() may fail due to network conditions or connection problems, resulting in residual error codes. In order to ensure the stability and robustness of the program, we need to clear the errors in time and try again after the read failure. At this time, the socket_clear_error() function comes in handy.
This article will introduce in detail how to combine socket_read() and socket_clear_error() for error handling and retry.
socket_read() is used to read data from a connected socket, with the syntax as follows:
socket_read(resource $socket, int $length, int $type = PHP_BINARY_READ): string|false
$socket : Socket resource.
$length : The maximum number of bytes to be read.
$type : read type, default to binary read.
If the read is successful, the read string will be returned; if it fails, false will be returned.
When socket_read() fails, an error status will be left inside the socket, which may affect subsequent operations. For example, socket_last_error() will return the last error code. If the error state is not cleared, the program may be in the error state all the time, resulting in repeated failures.
socket_clear_error() is used to clear the socket error status and ensure that subsequent operations can be executed normally.
The following is a sample code showing how to clear the error and try to retry the read after socket_read () fails.
<?php
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
die("create socket fail: " . socket_strerror(socket_last_error()) . "\n");
}
// Connect to the server(Assume server address and port)
$server = "m66.net";
$port = 12345;
if (!socket_connect($socket, $server, $port)) {
die("Connect to the serverfail: " . socket_strerror(socket_last_error($socket)) . "\n");
}
$maxRetries = 3; // Maximum number of retry
$retryCount = 0;
$readLength = 1024;
while ($retryCount < $maxRetries) {
$data = socket_read($socket, $readLength);
if ($data === false) {
// 读取fail,Get the error code
$errorCode = socket_last_error($socket);
$errorMsg = socket_strerror($errorCode);
echo "socket_read fail,Error code:$errorCode,error message:$errorMsg\n";
// Clear error
socket_clear_error($socket);
echo "Cleared socket mistake,Ready to try again...\n";
$retryCount++;
// Optional:Try again after sleeping for a while,Avoid frequent requests
sleep(1);
continue;
}
// Read successfully,Output data and jump out of loop
echo "Read data:" . $data . "\n";
break;
}
if ($retryCount === $maxRetries) {
echo "The number of retry has reached the maximum,读取fail。\n";
}
// closure socket
socket_close($socket);
?>
Create and connect to socket : Here we connect to a port of m66.net .
Loop attempts to read data : At most 3 reads are attempted, and if it fails, error processing is performed.
Failure handling :
Get the error code through socket_last_error() .
Use socket_strerror() to get the error description.
Call socket_clear_error() to clear the error state.
Wait for one second before continuing to try again.
If the read successfully, the data will be printed and the loop will be exited .
Close the connection and release the resource .
When you encounter failure when reading data using socket_read() , don't forget to call socket_clear_error() to clear the error. This can avoid error accumulation and ensure the correct execution of the next operation. At the same time, combined with the retry mechanism, the stability of network communication can be improved.