Current Location: Home> Function Categories> socket_get_option

socket_get_option

Get socket options for sockets
Name:socket_get_option
Category:Sockets
Programming Language:php
One-line Description:Get the value of the socket option

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:

  • $socket: Socket resource, created by socket_create() or socket_accept() functions.
  • $level: The level of the option, which can be SOL_SOCKET or other specific protocol level.
  • $optname: The option name, which can be constants such as SO_REUSEADDR, SO_RCVBUF.

Return value:

  • If successful, return the value of the option.
  • If it fails, return FALSE.

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.

Similar Functions
Popular Articles