Current Location: Home> Latest Articles> How to use socket_last_error() + socket_clear_error() to implement error code filtering

How to use socket_last_error() + socket_clear_error() to implement error code filtering

M66 2025-05-27

Error management is an important part of debugging and maintaining network communication programs when programming with PHP. socket_last_error() and socket_clear_error() are two functions provided by PHP for error handling. They can help developers get and clear the most recent socket error information. This article will explain in detail how to use these two functions and use an example to demonstrate how to filter error codes and clear error information.

1. socket_last_error() function

socket_last_error() is used to return the error code of the last socket operation. Its prototype is as follows:

 int socket_last_error ([ resource $socket ] )
  • The parameter $socket is optional. If a specific socket resource is passed into, the error code of the socket will be returned; if it is not passed, the error code that occurred the default is returned.

  • The return value is an integer indicating the error code.

2. socket_clear_error() function

socket_clear_error() is used to clear the most recent socket error record. The prototype is as follows:

 void socket_clear_error ([ resource $socket ] )
  • The parameter $socket is also optional. If not passed, clear the global socket error; if a specific socket resource is passed, clear the error record of the resource.

3. Practical application examples

The following is a simple TCP client connection example that illustrates how to use these two functions to capture, filter and clear errors.

 <?php
// Create a TCP socket
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

// Illegally connect to an address that does not exist,Triggering error
$host = 'm66.net';
$port = 9999; // Assume that this port has no service listening
@socket_connect($socket, $host, $port);

// Get the error code
$errorCode = socket_last_error($socket);

if ($errorCode !== 0) {
    // Get error message
    $errorMessage = socket_strerror($errorCode);
    echo "Connection failed,Error code: $errorCode,error message: $errorMessage\n";

    // 根据Error code进行逻辑判断
    if ($errorCode === 111 || $errorCode === 10061) {
        echo "The target host refuses to connect,examine m66.net:$port Whether to enable the service。\n";
    } elseif ($errorCode === 110 || $errorCode === 10060) {
        echo "Connection timeout,Please confirm m66.net Is it accessible。\n";
    }

    // Clear the error status
    socket_clear_error($socket);
}

// Continue program logic...
socket_close($socket);
?>

In the above code:

  1. Use @socket_connect to suppress the default warning message;

  2. Get the specific error code through socket_last_error() ;

  3. Use socket_strerror() to convert error codes into human-readable strings;

  4. Use the error code to determine whether the connection is rejected, timed out or other problems;

  5. Finally, use socket_clear_error() to clear the error to avoid subsequent misjudgments.

4. Common error code reference