MENU
Initialization
socket_create($i1, $i2, $i3) creates and returns a socket resource. socket_create_pair($i1, $i2, $i3, &$arr) creates two connected, indistinguishable sockets and stores them in $arr. The latter function is often used in IPC (Interprocess Communication).$i1 | |
AF_INET | IPv4 |
AF_INET6 | IPv6 |
AF_UNIX | Efficient local communication (for Interprocess Communication) |
$i2 | |
SOCK_STREAM | Sequenced, reliable, full-duplex, connection-based byte streams. (TCP) |
SOCK_DGRAM | Connectionless, unreliable datagrams of fixed maximum length. (UDP) |
SOCK_SEQPACKET | Sequenced, reliable, two-way connection-based datagrams of fixed maximum length. |
SOCK_RAW | Raw protocol access used to construct any type of protocol such as ICMP requests. |
SOCK_RDM | Datagram layer that does not guarantee ordering. Probably not implemented on your operating system. |
$i3 | |
SOL_TCP | TCP is reliable, connection-based, stream oriented, and full-duplexed. |
SOL_UDP | UDP is connectionless, unreliable, and uses fixed record lengths, requiring a minimum amount of protocol overhead. |
(icmp) | ICMP is mainly used to report errors in datagram communication. The ‘ping’ command is an example of ICMP. The integral parameter should be retrieved with getprotobyname(‘icmp’). |
socket_bind($r, $s [, $i=0]) binds the address $s and the port $i to the socket $r. If AF_INET is used, $s is in dotted-quad notation (eg. 127.0.0.1); if AF_UNIX is used, $s is the path of a Unix-domain socket (eg. Tmp/my.sock). TRUE is returned on success and FALSE on failure.
socket_set_option($r, $i1, $i2, $m) sets, for socket $r, at protocol level $i1, the option $i2 to a value of $m. For $i1, SOL_SOCKET is used for the socket level, and the protocol numbers (by getprotobyname()) for other levels. TRUE is returned on success and FALSE on failure:
$i2 | |
SO_DEBUG (int): | whether debugging information is being recorded. |
SO_BROADCAST (int): | whether the transmission of broadcast messages is supported. |
SO_REUSEADDR (int): | whether local addresses can be reused. |
SO_KEEPALIVE (int): | 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 the processes writing to that socket are notified with a SIGPIPE signal. |
SO_LINGER (array with two keys: | l_onoff and l_linger): 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. 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. |
SO_OOBINLINE (int): | whether the socket leaves out-of-band data inline. |
SO_SNDBUF (int): | the size of the send buffer. |
SO_RCVBUF (int): | the size of the receive buffer. |
SO_ERROR (int): | information about error status and clears it, for socket_get_option() only. |
SO_TYPE (int): | the socket type (eg. SOCK_STREAM), for socket_get_option() only. |
SO_DONTROUTE (int): | whether outgoing messages bypass the standard routing facilities. |
SO_RCVLOWAT (int): | the minimum number of bytes to process for socket input operations. |
SO_RCVTIMEO (array with two keys: | sec, for seconds part, and usec, for microseconds part): the timeout value for input operations. |
SO_SNDTIMEO (as in SO_RECVTIMEO): | the timeout value specifying the amount of time that an output function blocks because flow control prevents data from being sent. |
SO_SNDLOWAT (int): | the minimum number of bytes to process for socket output operations. |
TCP_NODELAY (int): | whether the Nagle TCP algorithm is disabled. |
MCAST_JOIN_GROUP (array with two keys: | group, for multicast address, and interface for interface number or name): joins a multicast group, for socket_set_option() only. |
MCAST_LEAVE_GROUP (array as in MCAST_JOIN_GROUP): | leaves a multicast group, for socket_set_option() only. |
MCAST_BLOCK_SOURCE | (array as in MCAST_JOIN_GROUP, plus the key source, which maps to a string specifying an IPv4 or IPv6 address of the source to be blocked): blocks packets arriving from a specific source to a specific multicast group, for socket_set_option() only. |
MCAST_UNBLOCK_SOURCE (array as in MCAST_BLOCK_ SOURCE): | unblocks packets arriving from a specific source address to a specific multicast group, for socket_set_option() only. |
MCAST_JOIN_SOURCE_GROUP (array as in MCAST_BLOCK_ SOURCE): | receives packets destined to a specific multicast group whose source address matches a specific value, for socket_set_option() only. |
MCAST_LEAVE_SOURCE_GROUP (array as in MCAST_BLOCK_ SOURCE): | stops receiving packets destined to a specific multicast group whose source address matches a specific value, for socket_set_option() only. |
IP_MULTICAST_IF | (int, for the interface number, or string with an interface name like eth0): the outgoing interface for IPv4 multicast packets. |
IPV6_MULTICAST_IF (as in IP_MULTICAST_IF): | the outgoing interface for IPv6 multicast packets. |
IP_MULTICAST_LOOP (int): | the multicast loopback policy for IPv4 packets, which determines whether multicast packets sent by this socket also reach receivers in the same host that have joined the same multicast group on the outgoing interface used by this socket. This is the case by default. |
IPV6_MULTICAST_LOOP (int): | analogous to IP_MULTICAST_ LOOP but for IPv6. |
IP_MULTICAST_TTL (int between 0 and 255): | the 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). |
IPV6_MULTICAST_HOPS (int between -1 and 255): | analogous to IP_MULTICAST_TTL, but for IPv6 packets. -1 is also accepted, meaning the route default should be used. |
socket_set_block($r) sets blocking mode on the socket $r. When an operation (receive, send, connect, accpt…) is performed on a blocking socket, the script will pause its execution until it receives a signal or it can perform the operation.
socket_set_nonblock ($r) is the opposite of the previous function. For these functions, TRUE is returned on success and FALSE on failure.
socket_import_stream($r) imports a stream that encapsulates a socket and returns an extension socket.
socket_close($r) closes the socket $r, which was created with socket_create() or socket_accept().