socket_get_option
(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)
Gets socket options for the socket
Description
The socket_get_option() function retrieves the value for the option specified by the option parameter for the specified socket.
Parameters
socketA
Socketinstance created withsocket_create()orsocket_accept().levelThe
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 thegetprotobyname()function.optionOption Description Type SO_DEBUGReports whether debugging information is being recorded. intSO_BROADCASTReports whether transmission of broadcast messages is supported. intSO_REUSEADDRReports whether local addresses can be reused. intSO_REUSEPORTReports whether local ports can be reused. intSO_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. intSO_LINGERReports whether the socketlingers onsocket_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.Ifl_onoffis non-zero andl_lingeris 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, ifl_onoffis non-zero andl_lingeris non-zero,socket_close()will block until all the data is sent or the time specified inl_lingerelapses. If the socket is non-blocking,socket_close()will fail and return an error.array. The array will contain two keys:l_onoffandl_linger.SO_OOBINLINEReports whether the socketleaves out-of-band data inline.intSO_SNDBUFReports the size of the send buffer. intSO_RCVBUFReports the size of the receive buffer. intSO_ERRORReports information about error status and clears it. int(cannot be set bysocket_set_option())SO_TYPEReports the sockettype (e.g.SOCK_STREAM).int(cannot be set bysocket_set_option())SO_DONTROUTEReports whether outgoing messages bypass the standard routing facilities. intSO_RCVLOWATReports the minimum number of bytes to process for socketinput operations.intSO_RCVTIMEOReports the timeout value for input operations. array. The array will contain two keys:secwhich is the seconds part on the timeout value andusecwhich 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:secwhich is the seconds part on the timeout value andusecwhich is the microsecond part of the timeout value.SO_SNDLOWATReports the minimum number of bytes to process for socketoutput operations.intTCP_NODELAYReports whether the Nagle TCP algorithm is disabled. intMCAST_JOIN_GROUPJoins a multicast group. arraywith keys"group", specifying astringwith an IPv4 or IPv6 multicast address and"interface", specifying either an interface number (typeint) or astringwith the interface name, like"eth0".0can be specified to indicate the interface should be selected using routing rules. (can only be used insocket_set_option())MCAST_LEAVE_GROUPLeaves a multicast group. array. SeeMCAST_JOIN_GROUPfor more information. (can only be used insocket_set_option())MCAST_BLOCK_SOURCEBlocks packets arriving from a specific source to a specific multicast group, which must have been previously joined. arraywith the same keys asMCAST_JOIN_GROUP, plus one extra key,source, which maps to astringspecifying an IPv4 or IPv6 address of the source to be blocked. (can only be used insocket_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. arraywith the same format asMCAST_BLOCK_SOURCE. (can only be used insocket_set_option())MCAST_JOIN_SOURCE_GROUPReceive packets destined to a specific multicast group whose source address matches a specific value. arraywith the same format asMCAST_BLOCK_SOURCE. (can only be used insocket_set_option())MCAST_LEAVE_SOURCE_GROUPStop receiving packets destined to a specific multicast group whose source address matches a specific value. arraywith the same format asMCAST_BLOCK_SOURCE. (can only be used insocket_set_option())IP_MULTICAST_IFThe outgoing interface for IPv4 multicast packets. Either intspecifying the interface number or astringwith an interface name, likeeth0. The value0can be used to indicate the routing table is to used in the interface selection. The functionsocket_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(either0or1). Forsocket_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. SeeIP_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). intbetween 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.intbetween -1 and 255.SO_MARKSets an identifier on the socket for packet filtering purpose on Linux. intSO_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). stringname of the filter (length 15 max).SO_USER_COOKIESets an identifier on the socket for packet filtering purpose on FreeBSD. intSO_RTABLESets an identifier on the socket for packet filtering purpose on OpenBSD. intSO_DONTTRUNCRetain unread data. intSO_WANTMOREGive a hint when more data is ready. intTCP_DEFER_ACCEPTDon't notify a listening socket until data is ready. intSO_INCOMING_CPUGets/Sets the cpu affinity of a socket. intSO_MEMINFOGets all the meminfo of a socket. intSO_BPF_EXTENSIONSGets the supported BPF extensions by the kernel to attach to a socket. intSO_SETFIBSets the route table (FIB) of a socket. (FreeBSD only) intSOL_FILTERFilters attributed to a socket. (Solaris/Illumos only) intTCP_KEEPCNTSets the maximum number of keepalive probes TCP should send before dropping the connection. intTCP_KEEPIDLESets the time the connection needs to remain idle. intTCP_KEEPINTVLSets the time between individual keepalive probes. intTCP_KEEPALIVESets the time the connection needs to remain idle. (macOS only) intTCP_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, Falseforfailure.
Changelog
| Version | Description |
|---|
Examples
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));
?>