Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Socket Functions : socket_recvfrom

socket_recvfrom

Receives data from a socket whether or not it is connection-oriented (PHP 4 >= 4.0.7, PHP 5)
int socket_recvfrom ( resource socket, string &buf, int len, int flags, string &name [, int &port] )

Example 2296. 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 an UDP socket on port 1223 of 127.0.0.1 and print at most 12 characters received from a remote host.


Code Examples / Notes » socket_recvfrom

tsuna

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() -> http://www.php.net/manual/en/function.socket-listen.php


jaggerwang

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

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);
}
?>


Change Language


Follow Navioo On Twitter
socket_accept
socket_bind
socket_clear_error
socket_close
socket_connect
socket_create_listen
socket_create_pair
socket_create
socket_get_option
socket_getpeername
socket_getsockname
socket_last_error
socket_listen
socket_read
socket_recv
socket_recvfrom
socket_select
socket_send
socket_sendto
socket_set_block
socket_set_nonblock
socket_set_option
socket_shutdown
socket_strerror
socket_write
eXTReMe Tracker