On this page
socket_get_option
(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)
socket_get_option — Gets socket options for the socket
Description
socket_get_option(Socket $socket, int $level, int $option): array|int|false
The socket_get_option() function retrieves the value for the option specified by the option parameter for the specified socket.
Parameters
socket-
A Socket instance created with socket_create() or socket_accept().
level-
The
levelparameter specifies the protocol level at which the option resides. For example, to retrieve options at the socket level, alevelparameter ofSOL_SOCKETwould be used. Other levels, such asTCP, can be used by specifying the protocol number of that level. Protocol numbers can be found by using the getprotobyname() function. option-
Available Socket Options Option Description Type 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
socketlingers 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 socketleaves 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 sockettype (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 socketinput 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 socketoutput 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 astringwith the interface name, like"eth0".0can 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_GROUPfor 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 betweenIP_MULTICAST_IFandIPV6_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 0or1). 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 Values
Returns the value of the given option, or false on failure.
Changelog
| Version | Description |
|---|---|
| 8.0.0 | socket is a Socket instance now; previously, it was a resource. |
Examples
Example #1 socket_get_option() example
<?php
$socket = socket_create_listen(1223);
$linger = array('l_linger' => 1, 'l_onoff' => 1);
socket_set_option($socket, SOL_SOCKET, SO_LINGER, $linger);
var_dump(socket_get_option($socket, SOL_SOCKET, SO_REUSEADDR));
?>
See Also
- socket_create_listen() - Opens a socket on port to accept connections
- socket_set_option() - Sets socket options for the socket
© 1997–2023 The PHP Documentation Group
Licensed under the Creative Commons Attribution License v3.0 or later.
https://www.php.net/manual/en/function.socket-get-option.php