Function name: socket_setopt()
Applicable version: PHP 4 >= 4.3.0, PHP 5, PHP 7
Function description: The socket_setopt() function is used to set the value of the socket option.
Syntax: bool socket_setopt ( resource $socket , int $level , int $optname , mixed $optval )
parameter:
Return value: Return true on success, and false on failure.
Example:
// 创建一个TCP socket $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); // 设置socket的超时时间为5秒$timeout = 5; socket_setopt($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $timeout, 'usec' => 0)); // 设置socket的发送缓冲区大小为8192字节$bufferSize = 8192; socket_setopt($socket, SOL_SOCKET, SO_SNDBUF, $bufferSize); // 设置socket的延迟关闭为1秒$delay = 1; socket_setopt($socket, SOL_SOCKET, SO_LINGER, array('l_onoff' => 1, 'l_linger' => $delay)); // 设置socket的重用地址选项为true socket_setopt($socket, SOL_SOCKET, SO_REUSEADDR, true); // 关闭socket socket_close($socket);
In the example above, a TCP socket is first created using the socket_create() function. Then use the socket_setopt() function to set several different options:
Finally, the socket was closed using the socket_close() function.
Please note that the specific option name and available values depend on the protocol and operating system used. It is recommended to consult the relevant documentation for specific options and values before using the socket_setopt() function.