socket_recvfrom

(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.

Список параметров

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, 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.

Возвращаемые значения

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.

Примеры

Пример #1 socket_recvfrom() example

<?php
error_reporting
(E_ALL E_STRICT);

$socket socket_create(AF_INETSOCK_DGRAMSOL_UDP);
socket_bind($socket'127.0.0.1'1223);

$from '';
$port 0;
socket_recvfrom($socket$buf120$from$port);

echo 
"Received $buf from remote address $from and remote port $portPHP_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.

Смотрите также

  • socket_recv() - Получает данные из подсоединённого сокета
  • socket_send() - Отправляет данные в подсоединённый сокет
  • socket_sendto() - Отправляет сообщение в сокет, независимо от того, подсоединён он или нет
  • socket_create() - Create a socket (endpoint for communication)

Коментарии

DNS RELAY USING UDP SOCKETS

<?php
 
while(TRUE) {
   
$socket socket_create(AF_INETSOCK_DGRAMSOL_UDP); 
   if(
$socket === FALSE
   { 
       echo 
'Socket_create failed: '.socket_strerror(socket_last_error())."\n";
   }
   if(!
socket_bind($socketD"0.0.0.0"53)) {
       
socket_close($socketD);
       echo 
'socket_bind failed: '.socket_strerror(socket_last_error())."\n";
   }
   
socket_recvfrom($socket,$buf,65535,0,$clientIP,$clientPort);
   
$stz bin2hex($buf);
   
$tx "";
   for(
$i=0;$i<(strlen($stz)-26-10)/2;$i++)
   {
     
$e "00";
     
$e[0] = $stz[$i*2+26];
     
$e[1] = $stz[$i*2+27];
     
$f hexdec($e);
     if(
$f && $f 32$tx .= "."; else
     
$tx .= sprintf("%c",$f);
   }
   echo 
"$clientIP <".$tx.">\n";                                           
   
$fp fsockopen("udp://72.174.110.4",53,$errno,$errstr);
   if (!
$fp
   {
       echo 
"ERROR: $errno - $errstr<br />\n";
   } 
   else 
   {
     
fwrite($fp,$buf);
     
$ret $buf;
     
$ret fread($fp,667);
     
fclose($fp);
   }
  }
socket_send($socket,$ret,667,0);
}
?>
2006-10-02 01:27:18
http://php5.kiev.ua/manual/ru/function.socket-recvfrom.html
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.
2007-11-07 23:56:07
http://php5.kiev.ua/manual/ru/function.socket-recvfrom.html
Pay attention! On some PHP version the MSG_DONTWAIT flag is not defined (see https://bugs.php.net/bug.php?id=48326)
2012-03-01 10:52:21
http://php5.kiev.ua/manual/ru/function.socket-recvfrom.html
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).
2015-08-22 01:24:32
http://php5.kiev.ua/manual/ru/function.socket-recvfrom.html
MSG_DONTWAIT doesn't seem to exist in windows sockets. However socket_set_nonblock() seems to do the trick.
2016-12-18 00:07:21
http://php5.kiev.ua/manual/ru/function.socket-recvfrom.html

    Поддержать сайт на родительском проекте КГБ