Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

socket_get_option

Description

The socket_get_option of Sockets for PHP gets socket options for the socket.

Syntax

socket_get_option(
    Socket $socket,
    int $level,
    int $option
): array|int|false

Parameters

socket

A Socket instance created with socket_create() or socket_accept().

level

The level parameter specifies the protocol level at which the option resides. For example, to retrieve options at the socket level, a level parameter of SOL_SOCKET would be used. Other levels, such as TCP, can be used by specifying the protocol number of that level. Protocol numbers can be found by using the getprotobyname() function.

option

The available socket options:

ConstantDescriptionType
SO_DEBUGReports whether debugging information is being recorded.int
SO_BROADCASTReports whether transmission of broadcast messages is supported.int
SO_REUSEADDRReports whether local addresses can be reused.int
SO_REUSEPORTReports whether local ports can be reused.int
SO_KEEPALIVEReports whether connections are kept active with periodic transmission of messages. If the connected socket fails to respond to these messages, the connection is broken and processes writing to that socket are notified with a SIGPIPE signal.int
SO_LINGERReports whether the socket lingers on socket_close() if data is present. By default, when the socket is closed, it attempts to send all unsent data. In the case of a connection-oriented socket, socket_close() will wait for its peer to acknowledge the data.

If l_onoff is non-zero and l_linger is zero, all the unsent data will be discarded and RST (reset) is sent to the peer in the case of a connection-oriented socket.

On the other hand, if l_onoff is non-zero and l_linger is non-zero, socket_close() will block until all the data is sent or the time specified in l_linger elapses. If the socket is non-blocking, socket_close() will fail and return an error.
array. The array will contain two keys: l_onoff and l_linger.
SO_OOBINLINEReports whether the socket leaves out-of-band data inline.int
SO_SNDBUFReports the size of the send buffer.int
SO_RCVBUFReports the size of the receive buffer.int
SO_ERRORReports information about error status and clears it.int (cannot be set by socket_set_option())
SO_TYPEReports the socket type (e.g. SOCK_STREAM).int (cannot be set by socket_set_option())
SO_DONTROUTEReports whether outgoing messages bypass the standard routing facilities.int
SO_RCVLOWATReports the minimum number of bytes to process for socket input operations.int
SO_RCVTIMEOReports the timeout value for input operations.array. The array will contain two keys: sec which is the seconds part on the timeout value and usec which is the microsecond part of the timeout value.
SO_SNDTIMEOReports the timeout value specifying the amount of time that an output function blocks because flow control prevents data from being sent.array. The array will contain two keys: sec which is the seconds part on the timeout value and usec which is the microsecond part of the timeout value.
SO_SNDLOWATReports the minimum number of bytes to process for socket output operations.int
TCP_NODELAYReports whether the Nagle TCP algorithm is disabled.int
MCAST_JOIN_GROUPJoins a multicast group.array with keys "group", specifying a string with an IPv4 or IPv6 multicast address and "interface", specifying either an interface number (type int) or a string with the interface name, like "eth0". 0 can be specified to indicate the interface should be selected using routing rules. (can only be used in socket_set_option())
MCAST_LEAVE_GROUPLeaves a multicast group.array. See MCAST_JOIN_GROUP for more information. (can only be used in socket_set_option())
MCAST_BLOCK_SOURCEBlocks packets arriving from a specific source to a specific multicast group, which must have been previously joined.array with the same keys as MCAST_JOIN_GROUP, plus one extra key, source, which maps to a string specifying an IPv4 or IPv6 address of the source to be blocked. (can only be used in socket_set_option())
MCAST_UNBLOCK_SOURCEUnblocks (start receiving again) packets arriving from a specific source address to a specific multicast group, which must have been previously joined.array with the same format as MCAST_BLOCK_SOURCE. (can only be used in socket_set_option())
MCAST_JOIN_SOURCE_GROUPReceive packets destined to a specific multicast group whose source address matches a specific value.array with the same format as MCAST_BLOCK_SOURCE. (can only be used in socket_set_option())
MCAST_LEAVE_SOURCE_GROUPStop receiving packets destined to a specific multicast group whose source address matches a specific value.array with the same format as MCAST_BLOCK_SOURCE. (can only be used in socket_set_option())
IP_MULTICAST_IFThe outgoing interface for IPv4 multicast packets.Either int specifying the interface number or a string with an interface name, like eth0. The value 0 can be used to indicate the routing table is to used in the interface selection. The function socket_get_option() returns an interface index. Note that, unlike the C API, this option does NOT take an IP address. This eliminates the interface difference between IP_MULTICAST_IF and IPV6_MULTICAST_IF.
IPV6_MULTICAST_IFThe outgoing interface for IPv6 multicast packets.The same as IP_MULTICAST_IF.
IP_MULTICAST_LOOPThe multicast loopback policy for IPv4 packets enables or disables loopback of outgoing multicasts, which must have been previously joined. The effect differs, however, whether it applies on unixes or Windows, the former being on the receive path whereas the latter being on the send path.int (either 0 or 1). For socket_set_option() any value will be accepted and will be converted to a boolean following the usual PHP rules.
IPV6_MULTICAST_LOOPAnalogous to IP_MULTICAST_LOOP, but for IPv6.int. See IP_MULTICAST_LOOP.
IP_MULTICAST_TTLThe time-to-live of outgoing IPv4 multicast packets. This should be a value between 0 (don't leave the interface) and 255. The default value is 1 (only the local network is reached).int between 0 and 255.
IPV6_MULTICAST_HOPSAnalogous to IP_MULTICAST_TTL, but for IPv6 packets. The value -1 is also accepted, meaning the route default should be used.int between -1 and 255.
SO_MARKSets an identifier on the socket for packet filtering purpose on Linux.int
SO_ACCEPTFILTERAdds an accept filter on the listened socket (FreeBSD/NetBSD). An accept filter kernel module needs to be loaded beforehand on FreeBSD (e.g. accf_http).string name of the filter (length 15 max).
SO_USER_COOKIESets an identifier on the socket for packet filtering purpose on FreeBSD.int
SO_RTABLESets an identifier on the socket for packet filtering purpose on OpenBSD.int
SO_DONTTRUNCRetain unread data.int
SO_WANTMOREGive a hint when more data is ready.int
TCP_DEFER_ACCEPTDon't notify a listening socket until data is ready.int
SO_INCOMING_CPUGets/Sets the cpu affinity of a socket.int
SO_MEMINFOGets all the meminfo of a socket.int
SO_BPF_EXTENSIONSGets the supported BPF extensions by the kernel to attach to a socket.int
SO_SETFIBSets the route table (FIB) of a socket. (FreeBSD only)int
SOL_FILTERFilters attributed to a socket. (Solaris/Illumos only)int
TCP_KEEPCNTSets the maximum number of keepalive probes TCP should send before dropping the connection.int
TCP_KEEPIDLESets the time the connection needs to remain idle.int
TCP_KEEPINTVLSets the time between individual keepalive probes.int
TCP_KEEPALIVESets the time the connection needs to remain idle. (macOS only)int
TCP_NOTSENT_LOWATSets the limit number of unsent data in write queue by the socket stream. (Linux only)int

Return

Returns the value of the given option, or false on failure.

Examples

1 · socket level option · SO_REUSEADDR

<?

$domain = AF_INET;
$type = SOCK_STREAM;
$protocol = SOL_TCP;

$socket = socket_create($domain, $type, $protocol);

    if($socket === false)
    {
        $error_code = socket_last_error();

        $socket_strerror = socket_strerror($error_code);

        die("socket_create: $socket_strerror");
    }

    $level = SOL_SOCKET;
    $option = SO_REUSEADDR;
    $value = 1;

    socket_set_option($socket, $level, $option, $value);

    $return = socket_get_option($socket, $level, $option);

    var_export($return);

socket_close($socket);

?>
1

2 · socket level option · SO_LINGER

<?

$domain = AF_INET;
$type = SOCK_STREAM;
$protocol = SOL_TCP;

$socket = socket_create($domain, $type, $protocol);

    if($socket === false)
    {
        $error_code = socket_last_error();

        $socket_strerror = socket_strerror($error_code);

        die("socket_create: $socket_strerror");
    }

    $level = SOL_SOCKET;
    $option = SO_LINGER;
    $value = array(
        'l_linger' => 1,
        'l_onoff' => 1
    );

    socket_set_option($socket, $level, $option, $value);

    $return = socket_get_option($socket, $level, $option);

    var_export($return);

socket_close($socket);

?>
array (
  'l_onoff' => 1,
  'l_linger' => 1,
)
HomeMenu