Function name: socket_set_option()
Function description: socket_set_option() function is used to set socket options.
Applicable version: This function is available in PHP 4 >= 4.3.0, PHP 5, PHP 7.
Syntax: bool socket_set_option ( resource $socket , int $level , int $optname , mixed $optval )
parameter:
Return value: Return true on success, and false on failure.
Example:
// 创建套接字$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); // 设置套接字选项if (socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1) === false) { echo "设置套接字选项失败: " . socket_strerror(socket_last_error()) . "\n"; // 处理错误情况} else { echo "套接字选项设置成功\n"; // 继续其他操作} // 关闭套接字socket_close($socket);
In the example above, a TCP socket is first created using the socket_create() function. The SO_REUSEADDR option is then set using the socket_set_option() function, which means that address reuse is allowed. If the setting option is successful, "Socket option setting is successful" will be output, otherwise the corresponding error message will be output.
Please note that the parameter values in the example are just schematics and need to be adjusted according to specific needs in actual applications.