socket_create_pair
Description
The socket_create_pair of Sockets for PHP creates a pair of indistinguishable sockets and stores them in an array.
Syntax
socket_create_pair( int $domain, int $type, int $protocol, array &$pair ): bool
Parameters
domain
The domain parameter specifies the protocol family to be used by the socket.
Constant | Description |
---|---|
AF_INET | IPv4 Internet based protocols. TCP and UDP are common protocols of this protocol family. |
AF_INET6 | IPv6 Internet based protocols. TCP and UDP are common protocols of this protocol family. |
AF_UNIX | Local communication protocol family. High efficiency and low overhead make it a great form of IPC (Interprocess Communication). |
type
The type parameter selects the type of communication to be used by the socket.
Constant | Description |
---|---|
SOCK_STREAM | Provides sequenced, reliable, full-duplex, connection-based byte streams. An out-of-band data transmission mechanism may be supported. The TCP protocol is based on this socket type. |
SOCK_DGRAM | Supports datagrams (connectionless, unreliable messages of a fixed maximum length). The UDP protocol is based on this socket type. |
SOCK_SEQPACKET | Provides a sequenced, reliable, two-way connection-based data transmission path for datagrams of fixed maximum length; a consumer is required to read an entire packet with each read call. |
SOCK_RAW | Provides raw network protocol access. This special type of socket can be used to manually construct any type of protocol. A common use for this socket type is to perform ICMP requests (like ping). |
SOCK_RDM | Provides a reliable datagram layer that does not guarantee ordering. This is most likely not implemented on your operating system. |
protocol
The protocol parameter sets the specific protocol within the specified domain to be used when communicating on the returned socket. The proper value can be retrieved by name by using getprotobyname(). If the desired protocol is TCP, or UDP the corresponding constants SOL_TCP, and SOL_UDP can also be used.
Constant | Name | Description |
---|---|---|
icmp | The Internet Control Message Protocol is used primarily by gateways and hosts to report errors in datagram communication. The "ping" command (present in most modern operating systems) is an example application of ICMP. | |
SOL_UDP | udp | The User Datagram Protocol is a connectionless, unreliable, protocol with fixed record lengths. Due to these aspects, UDP requires a minimum amount of protocol overhead. |
SOL_TCP | tcp | The Transmission Control Protocol is a reliable, connection based, stream oriented, full duplex protocol. TCP guarantees that all data packets will be received in the order in which they were sent. If any packet is somehow lost during communication, TCP will automatically retransmit the packet until the destination host acknowledges that packet. For reliability and performance reasons, the TCP implementation itself decides the appropriate octet boundaries of the underlying datagram communication layer. Therefore, TCP applications must allow for the possibility of partial record transmission. |
pair
Reference to an array in which the two Socket instances will be inserted.
Return
Returns true on success or false on failure.
Examples
1 · domain type protocol pair
<? $domain = AF_UNIX; $type = SOCK_STREAM; $protocol = 0; $return = socket_create_pair($domain, $type, $protocol, $pair); if($return === false) { $error_code = socket_last_error(); $socket_strerror = socket_strerror($error_code); die("socket_create_pair: $socket_strerror"); } var_export($return); socket_close($pair[0]); socket_close($pair[1]);
true
2 · socket_write socket_read
<? $domain = AF_UNIX; $type = SOCK_STREAM; $protocol = 0; $return = socket_create_pair($domain, $type, $protocol, $pair); if($return === false) { $error_code = socket_last_error(); $socket_strerror = socket_strerror($error_code); die("socket_create_pair: $socket_strerror"); } $data = 'data'; $socket_write = socket_write($pair[0], $data); if($socket_write === false) { $error_code = socket_last_error($pair[0]); $socket_strerror = socket_strerror($error_code); die("socket_write: $socket_strerror"); } echo "socket_write: $socket_write\n"; $length = strlen($data); $socket_read = socket_read($pair[1], $length); if($socket_read === false) { $error_code = socket_last_error($pair[1]); $socket_strerror = socket_strerror($error_code); die("socket_read: $socket_strerror"); } echo "socket_read: $socket_read"; socket_close($pair[0]); socket_close($pair[1]);
socket_write: 4 socket_read: data
3 · InterProcess Communication
<? $string1 = 'message from parent'; $string2 = 'message from child'; $domain = AF_UNIX; $type = SOCK_STREAM; $protocol = 0; if(socket_create_pair($domain, $type, $protocol, $pair) === false) { echo "socket_create_pair: ".socket_strerror(socket_last_error()); } $pid = pcntl_fork(); if($pid == -1) { echo 'could not fork process'; } elseif($pid) { //parent socket_close($pair[0]); if(socket_write($pair[1], $string1) === false) { echo "socket_write: ".socket_strerror(socket_last_error($pair[1])); } if(socket_read($pair[1], strlen($string2)) == $string2) { echo "received $string2\n"; } socket_close($pair[1]); } else { //child socket_close($pair[1]); if(socket_write($pair[0], $string2) === false) { echo "socket_write: ".socket_strerror(socket_last_error($pair[0])); } if(socket_read($pair[0], strlen($string1)) == $string1) { echo "received $string1\n"; } socket_close($pair[0]); }
Links
Sockets
- socket_accept
- socket_addrinfo_bind
- socket_addrinfo_connect
- socket_addrinfo_explain
- socket_addrinfo_lookup
- socket_atmark
- socket_bind
- socket_clear_error
- socket_close
- socket_cmsg_space
- socket_connect
- socket_create
- socket_create_listen
- socket_export_stream
- socket_get_option
- socket_getopt
- socket_getpeername
- socket_getsockname
- socket_import_stream
- socket_last_error
- socket_listen
- socket_read
- socket_recv
- socket_recvfrom
- socket_recvmsg
- socket_select
- socket_send
- socket_sendmsg
- socket_sendto
- socket_set_block
- socket_set_nonblock
- socket_set_option
- socket_setopt
- socket_shutdown
- socket_strerror
- socket_write
- socket_wsaprotocol_info_export
- socket_wsaprotocol_info_import
- socket_wsaprotocol_info_release