Handling socket errors
socket_last_error can be used to get the error ID of the last error from the sockets extension.
socket_strerror can be used to convert the ID to human-readable strings.
TCP client socket
Creating a socket that uses the TCP (Transmission Control Protocol)
Make sure the socket is successfully created. The
onSocketFailure function comes from Handling socket errors example in this topic.
Connect the socket to a specified address
The second line fails gracefully if connection failed.
Sending data to the server
socket_write function sends bytes through a socket. In PHP, a byte array is represented by a string, which is normally encoding-insensitive.
Receiving data from the server
The following snippet receives some data from the server using the
PHP_NORMAL_READ as the third parameter reads until a
\n byte, and this byte is included in the return value.
PHP_BINARY_READ, on the contrary, reads the required amount of data from the stream.
socket_set_nonblock was called in prior, and
PHP_BINARY_READ is used,
socket_read will return
false immediately. Otherwise, the method blocks until enough data (to reach the length in the second parameter, or to reach a line ending) are received, or the socket is closed.
This example reads data from a supposedly IRC server.
Closing the socket
Closing the socket frees the socket and its associated resources.
TCP server socket
Create a socket that uses the TCP. It is the same as creating a client socket.
Bind connections from a given network (parameter 2) for a specific port (parameter 3) to the socket.
The second parameter is usually
"0.0.0.0", which accepts connection from all networks. It can also
One common cause of errors from
socket_bind is that the address specified is already bound to another process. Other processes are usually killed (usually manually to prevent accidentally killing critical processes) so that the sockets would be freed.
Set a socket to listening
Make the socket listen to incoming connections using
socket_listen. The second parameter is the maximum number of connections to allow queuing before they are accepted.
A TCP server is actually a server that handles child connections.
socket_accept creates a new child connection.
Data transferring for a connection from
socket_accept is the same as that for a TCP client socket.
When this connection should be closed, call
socket_close($conn); directly. This will not affect the original TCP server socket.
Closing the server
On the other hand,
socket_close($socket); should be called when the server is no longer used. This will free the TCP address as well, allowing other processes to bind to the address.
UDP server socket
A UDP (user datagram protocol) server, unlike TCP, is not stream-based. It is packet-based, i.e. a client sends data in units called "packets" to the server, and the client identifies clients by their address. There is no builtin function that relates different packets sent from the same client (unlike TCP, where data from the same client are handled by a specific resource created by
socket_accept). It can be thought as a new TCP connection is accepted and closed every time a UDP packet arrives.
Creating a UDP server socket
Binding a socket to an address
The parameters are same as that for a TCP server.
Sending a packet
This line sends
$data in a UDP packet to
Receiving a packet
The following snippet attempts to manage UDP packets in a client-indexed manner.
Closing the server
socket_close can be used on the UDP server socket resource. This will free the UDP address, allowing other processes to bind to this address.