The Server Pages
:: TSP Menu
- Home
- Authors
- Glossary
- RSS Feed

:: Servers
- CPanel
- Hosting
- Linux
- Windows

:: Webmaster
- Databases
- Design
- Search Engines

:: Scripts
- Scripts

:: Documentation
- PHP Manual

:: Other
- Errors & Solutions
- TechPunt
- Wojjie

search for in the

socket_select> <socket_recv
[edit] Last updated: Sat, 12 May 2012

view this page in


(PHP 4 >= 4.1.0, PHP 5)

socket_recvfromReceives data from a socket whether or not it is connection-oriented


int socket_recvfrom ( resource $socket , string &$buf , int $len , int $flags , string &$name [, int &$port ] )

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.



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


The data received will be fetched to the variable specified with buf.


Up to len 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 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.

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, or NULL if the socket is connection-oriented.


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.


Example #1 socket_recvfrom() example


$socket socket_create(AF_INETSOCK_DGRAMSOL_UDP);

$from '';
$port 0;

"Received $buf from remote address $from and remote port $portPHP_EOL;

This example will initiate a UDP socket on port 1223 of and print at most 12 characters received from a remote host.


Version Description
4.3.0 socket_recvfrom() is now binary safe.

See Also

socket_select> <socket_recv
[edit] Last updated: Sat, 12 May 2012
add a note add a note User Contributed Notes socket_recvfrom
davide dot renzi at gmail dot com 01-Mar-2012 08:52
Pay attention! On some PHP version the MSG_DONTWAIT flag is not defined (see
rmayo100 at yahoo dot com 16-Apr-2010 07:13
I said in my previous note that I'd add a secondary post when I found out more about how to use socket_recvfrom().  So here it is.

As I suspected, the example given is dead-flat-wrong and should be avoided.  You WILL need to use pointers like the description at the top of the page says.  My working socket_recvfrom() command looks like this:

     $bytes = socket_recvfrom ($socket, &$udp_reply, 100, 0, &$from_addr, &$from_port);

Also, even if you have no interest in the the information at all, you must create the $from_addr and $from_port variables and pass them by reference.  PHP doesn't support passing in NULL just because you might not give a rat's patootie about the information....    ;)

rmayo100 at yahoo dot com 14-Apr-2010 10:27
The example doesn't match the format described at the top of the page.  It appears to me that to use this command to receive UDP data, I need to format my calling parameter as follows:

int socket_recvfrom
    resource $socket,        //  Created this with socket_create
    string     &$buf,        //   This is a pointer!!
    int         $len,        //    I know the amount of data
    int         $flags,      //   I know the flags I want
    string     &$name,       //   This is ALSO a pointer!
    int         &$port       //   This is ALSO a pointer, TOO!!

The example doesn't use any pointers, just values, and I didn't think socket_bind was necessary for UDP sockets.  At least, it isn't in C.  Anybody got a example that does work??  (Because this doesn't see to so far.)

If I do stumble on a combination of settings that works, I'll post it -- but this doesn't seem to be the most active page on

jaggerwang at gmail dot com 07-Nov-2007 09:56
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 01-Oct-2006 10:27

while(TRUE) {
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
$socket === FALSE)
'Socket_create failed: '.socket_strerror(socket_last_error())."\n";
socket_bind($socketD, "", 53)) {
'socket_bind failed: '.socket_strerror(socket_last_error())."\n";
$stz = bin2hex($buf);
$tx = "";
$e = "00";
$e[0] = $stz[$i*2+26];
$e[1] = $stz[$i*2+27];
$f = hexdec($e);
$f > 0 && $f < 32) $tx .= "."; else
$tx .= sprintf("%c",$f);
"$clientIP <".$tx.">\n";                                           
$fp = fsockopen("udp://",53,$errno,$errstr);
   if (!
"ERROR: $errno - $errstr<br />\n";
$ret = $buf;
$ret = fread($fp,667);
tsuna at tsunaquack d0t c0m 21-Feb-2005 03:17
This function is very handy when dealing with UDP connections, because it enables you to know who's the client who connected to your socket. Bear in mind that UDP doesn't care about the source of the connection, the packets may be annonymous or even faked. No check is required.

If you want to listen on an UDP socket and answer to the client, read my comment on socket_listen() ->

show source | credits | sitemap | contact | advertising | mirror sites