(PHP 4 >= 4.1.0, PHP 5)
socket_recvfrom — Receives data from a socket whether or not it is connection-oriented
The socket_recvfrom() function receives
len bytes of data in
name on port
port (if the
socket is not of type
socket. socket_recvfrom() can be
used to gather data from both connected and unconnected sockets.
Additionally, one or more flags can be specified to modify the behaviour of
port must be
passed by reference. If the socket is not connection-oriented,
name will be set to the internet protocol address of
the remote host or the path to the UNIX socket. If the socket is
port will contain the port of the remote host in
the case of an unconnected
socketmust be a socket resource previously created by socket_create().
The data received will be fetched to the variable specified with
lenbytes will be fetched from remote host.
The value of
flagscan be any combination of the following flags, joined with the binary OR (|) operator.
Possible values for
Process out-of-band data.
Receive data from the beginning of the receive queue without removing it from the queue.
Block until at least
lenare received. However, if a signal is caught or the remote host disconnects, the function may return less data.
With this flag set, the function returns even if it would normally have blocked.
If the socket is of the type
nameis the path to the file. Else, for unconnected sockets,
nameis the IP address of, the remote host, or
NULLif the socket is connection-oriented.
This argument only applies to
AF_INET6sockets, and specifies the remote port from which the data is received. If the socket is connection-oriented,
socket_recvfrom() returns the number of bytes received,
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.
Example #1 socket_recvfrom() example
error_reporting(E_ALL | E_STRICT);
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_bind($socket, '127.0.0.1', 1223);
$from = '';
$port = 0;
socket_recvfrom($socket, $buf, 12, 0, $from, $port);
echo "Received $buf from remote address $from and remote port $port" . PHP_EOL;
This example will initiate a UDP socket on port 1223 of 127.0.0.1 and print at most 12 characters received from a remote host.
|4.3.0||socket_recvfrom() is now binary safe.|