socket_recvfrom
(PHP 4 >= 4.1.0, PHP 5, PHP 7)
socket_recvfrom — Receives data from a socket whether or not it is connection-oriented
Description
$socket
, string &$buf
, int $len
, int $flags
, string &$name
[, int &$port
] ) : int
The socket_recvfrom() function receives
len
bytes of data in buf
from
name
on port port
(if the
socket is not of type AF_UNIX
) using
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
the function.
The name
and 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
connection-oriented, name
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 resource previously created by socket_create(). -
buf
-
The data received will be fetched to the variable specified with
buf
. -
len
-
Up to
len
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.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 len
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. -
name
-
If the socket is of the type
AF_UNIX
type,name
is the path to the file. Else, for unconnected sockets,name
is the IP address of, the remote host, orNULL
if the socket is connection-oriented. -
port
-
This argument only applies to
AF_INET
andAF_INET6
sockets, and specifies the remote port from which the data is received. If the socket is connection-oriented,port
will beNULL
.
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.
Examples
Example #1 socket_recvfrom() example
<?php
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.
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)
English translation
You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.
Thank you in advance.
Document created the 30/01/2003, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/php-rf-socket-recvfrom.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.