socket_get_option
Get socket options for sockets
Function name: socket_get_option()
Applicable version: PHP 4 >= 4.3.0, PHP 5, PHP 7
Function description: socket_get_option() function is used to get the value of the socket option.
Syntax: mixed socket_get_option ( resource $socket , int $level , int $optname )
parameter:
Return value:
Example:
// 创建套接字$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); if ($socket === false) { echo "socket_create() failed: " . socket_strerror(socket_last_error()) . "\n"; exit; } // 设置套接字选项socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1); // 获取套接字选项的值$reuse = socket_get_option($socket, SOL_SOCKET, SO_REUSEADDR); if ($reuse === false) { echo "socket_get_option() failed: " . socket_strerror(socket_last_error()) . "\n"; exit; } echo "SO_REUSEADDR option value: " . $reuse . "\n"; // 关闭套接字socket_close($socket);
In the above example, we first create a socket, and then use the socket_set_option() function to set the value of the SO_REUSEADDR option to 1. Finally, use the socket_get_option() function to get the value of the SO_REUSEADDR option and output it to the console. Please note that the error handling in the example is basic, and appropriate error handling should be carried out in actual applications as needed.