In PHP network programming, socket_set_option is a very critical function. It is used to set socket options to control underlying behavior, such as timeout, buffer size, blocking mode, etc. However, many developers encounter common problems when using this function, affecting the stability and performance of the program. This article will introduce these common errors in detail and provide practical troubleshooting and repair suggestions.
Problem description:
The first parameter of socket_set_option must be a valid socket resource. If a non-resource type (such as a boolean false or a string) is passed in, the function call will fail.
Error example:
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if (!$socket) {
echo "Socket create failed.\n";
}
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1); // $socket may be false
Fix method:
Make sure socket_create successfully returns a valid resource and performs error checking:
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
die("Socket creation failed: " . socket_strerror(socket_last_error()));
}
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
Problem description:
When setting options, the second and third parameters are usually constants (such as SOL_SOCKET , SO_REUSEADDR , etc.). An input error or a misspelling will cause the function to not recognize the options.
Error example:
socket_set_option($socket, SOL_SOCKT, SO_REUSEADDR, 1); // Error spelling
Fix method:
Always use PHP built-in constants and enable error reports for quick location:
error_reporting(E_ALL);
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
Problem description:
Some operating systems do not support certain options, such as some Linux systems do not support SO_RCVTIMEO setting to negative values, or certain TCP layer options are not supported.
Fix method:
Check the system's socket support documentation or man manual.
Use socket_get_option to verify that the option is effective.
For timeout settings, it is recommended to specify the millisecond value explicitly:
$timeout = ['sec' => 5, 'usec' => 0];
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, $timeout);
Problem description:
Some socket options require administrator permissions, such as when trying to modify broadcast permissions and binding restricted ports.
Fix method:
Run PHP programs as a user with sufficient permissions, or avoid using privileged ports:
socket_set_option($socket, SOL_SOCKET, SO_BROADCAST, 1);
If there is a permission error, you can view the system log or use socket_last_error() to get detailed information.
Error prompt on:
error_reporting(E_ALL);
ini_set('display_errors', 1);
Use socket_last_error and socket_strerror to get the details:
if (!socket_set_option($socket, SOL_SOCKET, SO_RCVBUF, 1024)) {
echo "Error: " . socket_strerror(socket_last_error($socket));
}
Step by step test:
Split the configuration into multiple steps and check it one by one:
$result1 = socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
$result2 = socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, ['sec' => 2, 'usec' => 0]);
View the system log:
If in Linux systems, you can use the following command to view the relevant logs:
dmesg | grep socket
Scenario: The user encounters an error of "Unable to reuse the address" when deploying the PHP Socket server.
Error code:
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($socket, '0.0.0.0', 8080); // Report an error Address already in use
Fix: Use SO_REUSEADDR to set it in advance:
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
socket_bind($socket, '0.0.0.0', 8080);
For more cases like this, please refer to: https://gitbox.net/docs/php-socket-options