socket_recvfrom

(PHP 4 >= 4.1.0, PHP 5, PHP 7, PHP 8)

socket_recvfromRecebe dados de um socket, conectado ou não

Descrição

socket_recvfrom ( resource $socket , string $&buf , int $len , int $flags , string $&name , int $&port = ? ) : int
Aviso

Esta função é EXPERIMENTAL. O comportamento, seu nome e documentação podem mudar sem aviso em futuras versões do PHP. Utilize por sua própria conta e risco.

Aviso

Esta função não está documentada; somente a lista de argumentos está disponível.

add a note add a note

User Contributed Notes 5 notes

up
3
lorin dot weilenmann at gmail dot com
8 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).
up
0
ply2attoetensen-project.com
7 years ago
MSG_DONTWAIT doesn't seem to exist in windows sockets. However socket_set_nonblock() seems to do the trick.
up
0
davide dot renzi at gmail dot com
12 years ago
Pay attention! On some PHP version the MSG_DONTWAIT flag is not defined (see https://bugs.php.net/bug.php?id=48326)
up
-4
jaggerwang at gmail dot com
16 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.
up
-10
ryan_at_ryanfisher_dot_com
17 years ago
DNS RELAY USING UDP SOCKETS

<?php

while(TRUE) {
  
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_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 > 0 && $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);
}
?>
To Top