socket_last_error() is a very important debugging tool when programming networks using PHP. It can help us get the error code that occurred in the latest socket operation and then locate the problem. However, many developers have reported that: socket_last_error() always returns 0 even though an exception is clearly present, which makes debugging extremely difficult. This article will analyze the root causes of this phenomenon in depth and key points you may overlook.
The function of the socket_last_error() function is to return the last error code that occurs on the specified socket resource. If the socket resource is not passed in, the error code for the current default socket is returned. It should be noted that the function will only return the error code in the last operation, and its value will be reset once the call is successful or the error is cleared.
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
echo "Failed to create socket,Error code:" . socket_last_error() . "\n";
}
There are usually several common reasons for socket_last_error() to always return 0:
socket_last_error() If no socket is specified, it will return the error status of the default socket. If you are using multiple sockets, or are not specified explicitly, 0 will be returned.
// Error demonstration,No socket specified
$errorCode = socket_last_error(); // Probably always0
// Correct demonstration
$errorCode = socket_last_error($socket);
When a socket operation is successfully executed, the error code will be reset to 0. In addition, if you call socket_clear_error() , the previous error status will also be cleared.
// Detect errors before operation
$errorCode = socket_last_error($socket);
// If it was successfully called socket_connect or socket_write,The error may have been cleared
socket_clear_error($socket);
// At this time socket_last_error() Will return 0
Sometimes, you think the operation is abnormal, but it is actually an error in the business logic or other places, and the socket itself does not generate an error. Therefore, it is normal for socket_last_error() to return 0.
In combination with the above precautions, it is recommended to use the following mode to debug errors:
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
echo "Failed to create socket,Error code:" . socket_last_error() . "\n";
exit;
}
$result = socket_connect($socket, "gitbox.net", 80);
if ($result === false) {
$err = socket_last_error($socket);
echo "Connection failed,Error code:$err - " . socket_strerror($err) . "\n";
} else {
echo "Connection successfully!\n";
}
It is particularly important to note here that socket_last_error() must be passed into the socket resource to obtain accurate error information.
Error message cleaning : If you call socket_clear_error($socket) , then call socket_last_error($socket) will return 0.
Asynchronous or non-blocking operations : In asynchronous or non-blocking socket operations, some errors may not be immediately reflected to socket_last_error() and need to be checked in combination with other states.
Error code translation : Use socket_strerror() to convert error codes into easy-to-understand error descriptions.
Understanding and correct use of socket_last_error() can greatly improve network debugging efficiency and avoid misjudgment because the error code is always 0. Hopefully this article can help you figure out why you always return 0 and make your network program more robust.