socket_create
Description
The socket_create of Sockets for PHP create a socket (endpoint for communication).
Syntax
socket_create( int $domain, int $type, int $protocol ): Socket|false
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().
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. |
Return
Returns a Socket instance on success, or false on error.
The actual error code can be retrieved by calling socket_last_error(). This error code may be passed to socket_strerror() to get a textual explanation of the error.
Examples
1 · domain · AF_INET
<? $domain = AF_INET; $type = SOCK_STREAM; $protocol = SOL_TCP; $return = socket_create($domain, $type, $protocol); if($return === false) { $error_code = socket_last_error(); $socket_strerror = socket_strerror($error_code); die("socket_create: $socket_strerror"); } var_export($return); socket_close($return);
\Socket::__set_state(array( ))
2 · domain · AF_INET6
<? $domain = AF_INET6; $type = SOCK_STREAM; $protocol = SOL_TCP; $return = socket_create($domain, $type, $protocol); if($return === false) { $error_code = socket_last_error(); $socket_strerror = socket_strerror($error_code); die("socket_create: $socket_strerror"); } var_export($return); socket_close($return);
\Socket::__set_state(array( ))
3 · domain · AF_UNIX
<? $domain = AF_UNIX; $type = SOCK_STREAM; $protocol = 0; $return = socket_create($domain, $type, $protocol); if($return === false) { $error_code = socket_last_error(); $socket_strerror = socket_strerror($error_code); die("socket_create: $socket_strerror"); } var_export($return); socket_close($return);
\Socket::__set_state(array( ))
4 · type · SOCK_STREAM
<? $domain = AF_INET; $type = SOCK_STREAM; $protocol = SOL_TCP; $return = socket_create($domain, $type, $protocol); if($return === false) { $error_code = socket_last_error(); $socket_strerror = socket_strerror($error_code); die("socket_create: $socket_strerror"); } var_export($return); socket_close($return);
\Socket::__set_state(array( ))
5 · type · SOCK_DGRAM
<? $domain = AF_INET; $type = SOCK_DGRAM; $protocol = SOL_UDP; $return = socket_create($domain, $type, $protocol); if($return === false) { $error_code = socket_last_error(); $socket_strerror = socket_strerror($error_code); die("socket_create: $socket_strerror"); } var_export($return); socket_close($return);
\Socket::__set_state(array( ))
6 · type · SOCK_SEQPACKET
<? $domain = AF_UNIX; $type = SOCK_SEQPACKET; $protocol = 0; $return = socket_create($domain, $type, $protocol); if($return === false) { $error_code = socket_last_error(); $socket_strerror = socket_strerror($error_code); die("socket_create: $socket_strerror"); } var_export($return); socket_close($return);
\Socket::__set_state(array( ))
7 · type · SOCK_RAW
<? $domain = AF_UNIX; $type = SOCK_RAW; $protocol = getprotobyname('icmp'); $return = socket_create($domain, $type, $protocol); if($return === false) { $error_code = socket_last_error(); $socket_strerror = socket_strerror($error_code); die("socket_create: $socket_strerror"); } var_export($return); socket_close($return);
\Socket::__set_state(array( ))
8 · type · SOCK_RDM
<? $domain = AF_INET; $type = SOCK_RDM; $protocol = 0; $return = socket_create($domain, $type, $protocol); if($return === false) { $error_code = socket_last_error(); $socket_strerror = socket_strerror($error_code); die("socket_create: $socket_strerror"); } var_export($return); socket_close($return);
socket_create: Socket type not supported
9 · protocol · icmp
<? $domain = AF_UNIX; $type = SOCK_RAW; $protocol = getprotobyname('icmp'); $return = socket_create($domain, $type, $protocol); if($return === false) { $error_code = socket_last_error(); $socket_strerror = socket_strerror($error_code); die("socket_create: $socket_strerror"); } var_export($return); socket_close($return);
\Socket::__set_state(array( ))
10 · protocol · SOL_UDP
<? $domain = AF_INET; $type = SOCK_DGRAM; $protocol = SOL_UDP; $return = socket_create($domain, $type, $protocol); if($return === false) { $error_code = socket_last_error(); $socket_strerror = socket_strerror($error_code); die("socket_create: $socket_strerror"); } var_export($return); socket_close($return);
\Socket::__set_state(array( ))
11 · protocol · SOL_TCP
<? $domain = AF_INET; $type = SOCK_STREAM; $protocol = SOL_TCP; $return = socket_create($domain, $type, $protocol); if($return === false) { $error_code = socket_last_error(); $socket_strerror = socket_strerror($error_code); die("socket_create: $socket_strerror"); } var_export($return); socket_close($return);
\Socket::__set_state(array( ))
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_listen
- socket_create_pair
- 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