[PHP 4 >= 4.1.0, PHP 5, PHP 7, PHP 8]
socket_recvfrom — Receives data from a socket whether or not it is connection-oriented
Description
socket_recvfrom[
Socket
$socket
,
string &$data
,
int $length
,
int $flags
,
string &$address
,
int &$port
= null
]: int|false
The address
and port
must be passed by reference. If the socket is not connection-oriented, address
will be set to the internet protocol address of the remote host or the path to the UNIX socket. If the socket is connection-oriented, address
is null
. Additionally, the port
will contain the port of the remote host in the case of an unconnected AF_INET
or AF_INET6
socket.
Note: This function is binary-safe.
Parameters
socket
The socket
must be a Socket instance previously created by socket_create[].
data
The data received will be fetched to the variable specified with data
.
length
Up to length
bytes will be fetched from
remote host.
flags
The value of flags
can be any combination of the following flags, joined with the binary OR [|
] operator.
flags
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. |
address
If the socket is of the type AF_UNIX
type, address
is the path to the file. Else, for unconnected sockets, address
is the IP address of, the remote host, or null
if the socket is connection-oriented.
port
This argument only applies to AF_INET
and AF_INET6
sockets, and specifies the remote port from which the data is received. If the
socket is connection-oriented, port
will be null
.
Return Values
socket_recvfrom[] 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.
Changelog
8.0.0 | socket is a Socket instance now; previously, it was a resource.
|
Examples
Example #1 socket_recvfrom[] example
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.
See Also
- socket_recv[] - Receives data from a connected socket
- socket_send[] - Sends data to a connected socket
- socket_sendto[] - Sends a message to a socket, whether it is connected or not
- socket_create[] - Create a socket [endpoint for communication]
lorin dot weilenmann at gmail dot com ¶
7 years ago
If you use socket_recvfrom on a UDP socket and combine it with the MSG_DONTWAIT flag, it will raise a PHP Warning if there is nothing to read. AFAIK, there is no way around that warning except suppressing it with @ [i.e. you cannot check if there is data before calling socket_recvfrom].
ply2attoetensen-project.com ¶
5 years ago
MSG_DONTWAIT doesn't seem to exist in windows sockets. However socket_set_nonblock[] seems to do the trick.
jaggerwang at gmail dot com ¶
14 years ago
I'm confused about the rerturn value of socket_recvfrom[], it said -1 when failed, but when I call like this:
if [[$len = @socket_recvfrom[$sock, $result, 32, 0, $ip, $port]] == -1] {
if [$this->_debug] {
echo "socket_read[] failed: " . socket_strerror[socket_last_error[]] . "\n";
}
return false;
}
variable $len = false, when I change the buffer length from 32 to 4096, it becomes right.
ryan_at_ryanfisher_dot_com ¶
15 years ago
DNS RELAY USING UDP SOCKETS