When using PHP for Socket-based programming, socket_accept() is a key function that accepts connections from clients. However, if the function call fails, an error may be thrown, causing the program to break or unpredictable behavior. Therefore, catching these errors and recording them in the log is very necessary for debugging and operation and maintenance.
This article will introduce in detail how to catch the errors of the socket_accept() function and log it into a log file.
Here is a basic Socket server example:
<?php
$host = '0.0.0.0';
$port = 8080;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
error_log("socket_create() failed: " . socket_strerror(socket_last_error()) . "\n", 3, "/var/log/php_socket_error.log");
exit;
}
if (!socket_bind($socket, $host, $port)) {
error_log("socket_bind() failed: " . socket_strerror(socket_last_error($socket)) . "\n", 3, "/var/log/php_socket_error.log");
exit;
}
if (!socket_listen($socket, 5)) {
error_log("socket_listen() failed: " . socket_strerror(socket_last_error($socket)) . "\n", 3, "/var/log/php_socket_error.log");
exit;
}
socket_accept() will return false on failure. To catch the error code and log the log, you can handle it as follows:
while (true) {
$clientSocket = @socket_accept($socket);
if ($clientSocket === false) {
$errCode = socket_last_error($socket);
$errMsg = socket_strerror($errCode);
error_log("socket_accept() failed with error [$errCode]: $errMsg\n", 3, "/var/log/php_socket_error.log");
// You can choose to continue or terminate the loop as needed
continue;
}
// Processing logic after receiving the connection
$msg = "Hello from server on http://m66.net\n";
socket_write($clientSocket, $msg, strlen($msg));
socket_close($clientSocket);
}
In this code:
The @ operator is used to suppress the default warning thrown by socket_accept() ;
socket_last_error() gets the error code of the last Socket operation;
socket_strerror() converts error code into human-readable strings;
error_log() writes errors to /var/log/php_socket_error.log file.
Note: Make sure you have permission to write to the log file path and that the parent directory of the log file exists.
You can also encapsulate the error log into a function for multiplexing and unified format output:
function log_socket_error($socket, $context = '') {
$errCode = socket_last_error($socket);
$errMsg = socket_strerror($errCode);
$timestamp = date('Y-m-d H:i:s');
$logEntry = "[$timestamp] [$context] Error [$errCode]: $errMsg\n";
error_log($logEntry, 3, "/var/log/php_socket_error.log");
}
How to use it is as follows:
$clientSocket = @socket_accept($socket);
if ($clientSocket === false) {
log_socket_error($socket, 'socket_accept');
continue;
}
Through the above method, you can effectively catch and record errors when using socket_accept() to avoid server silent failure or debugging difficulties. At the same time, writing logs to files also facilitates subsequent operation and maintenance checks and integration of automatic monitoring systems. In actual production environments, it is also recommended to use similar ELK or other centralized logging schemes to further enhance observability.