PHP 8.1.28 Released!

socket_recvfrom

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

socket_recvfromRiceve i dati da un socket, che sia connesso o meno

Descrizione

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

Questa funzione è SPERIMENTALE. Ovvero, il comportamento di questa funzione, il nome di questa funzione, in definitiva tutto ciò che è documentato qui può cambiare nei futuri rilasci del PHP senza preavviso. Siete avvisati, l'uso di questa funzione è a vostro rischio.

Avviso

Questa funzione, al momento non è documentata; è disponibile soltanto la lista degli argomenti.

La funzione socket_recvfrom() gestisce i dati binari dal PHP 4.3.0

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
1
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
1
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
-3
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
-13
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