PHP 4 >= 4.1.0, PHP 5, PHP 7, PHP 8
socket_recv - Receives data from a connected socket
Code Examples

socket_recv( Socket$socket, string|nulldata, int$length, int$flags ): int|false

The socket_recv function receives length bytes of data in data from socket. socket_recv can be used to gather data from connected sockets. Additionally, one or more flags can be specified to modify the behaviour of the function.

data is passed by reference, so it must be specified as a variable in the argument list. Data read from socket by socket_recv will be returned in data.



The socket must be a Socket instance previously created by socket_create().


The data received will be fetched to the variable specified with data. If an error occurs, if the connection is reset, or if no data is available, data will be set to null.


Up to length bytes will be fetched from remote host.


The value of flags can be any combination of the following flags, joined with the binary OR (|) operator.

Possible values for flags
Flag Description
MSG_OOB Process out-of-band data.
MSG_PEEK Receive data from the beginning of the receive queue without removing it from the queue.
MSG_WAITALL Block until at least length are received. However, if a signal is caught or the remote host disconnects, the function may return less data.
MSG_DONTWAIT With this flag set, the function returns even if it would normally have blocked.

Return Values

socket_recv returns the number of bytes received, or false if there was an 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.


Version Description
8.0.0 socket is a Socket instance now; previously, it was a resource.

Example of socket_recv

Show all examples for socket_recv

PHP Version: