1--TEST-- 2Test if socket_recvfrom() receives data sent by socket_sendto() via IPv4 UDP 3--SKIPIF-- 4<?php 5if (!extension_loaded('sockets')) { 6 die('SKIP The sockets extension is not loaded.'); 7} 8--FILE-- 9<?php 10 $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); 11 if (!$socket) { 12 die('Unable to create AF_INET socket'); 13 } 14 if (!socket_set_nonblock($socket)) { 15 die('Unable to set nonblocking mode for socket'); 16 } 17 18 $address = '127.0.0.1'; 19 socket_sendto($socket, '', 1, 0, $address); // cause warning 20 if (!socket_bind($socket, $address, 1223)) { 21 die("Unable to bind to $address:1223"); 22 } 23 24 var_dump(socket_recvfrom($socket, $buf, 12, 0, $from, $port)); //false (EAGAIN - no warning) 25 26 $msg = "Ping!"; 27 $len = strlen($msg); 28 $bytes_sent = socket_sendto($socket, $msg, $len, 0, $address, 1223); 29 if ($bytes_sent == -1) { 30 die('An error occurred while sending to the socket'); 31 } else if ($bytes_sent != $len) { 32 die($bytes_sent . ' bytes have been sent instead of the ' . $len . ' bytes expected'); 33 } 34 35 $from = ""; 36 $port = 0; 37 socket_recvfrom($socket, $buf, 12, 0); // cause warning 38 socket_recvfrom($socket, $buf, 12, 0, $from); // cause warning 39 $bytes_received = socket_recvfrom($socket, $buf, 12, 0, $from, $port); 40 if ($bytes_received == -1) { 41 die('An error occurred while receiving from the socket'); 42 } else if ($bytes_received != $len) { 43 die($bytes_received . ' bytes have been received instead of the ' . $len . ' bytes expected'); 44 } 45 echo "Received $buf from remote address $from and remote port $port" . PHP_EOL; 46 47 socket_close($socket); 48--EXPECTF-- 49 50Warning: Wrong parameter count for socket_sendto() in %s on line %d 51bool(false) 52 53Warning: socket_recvfrom() expects at least 5 parameters, 4 given in %s on line %d 54 55Warning: Wrong parameter count for socket_recvfrom() in %s on line %d 56Received Ping! from remote address 127.0.0.1 and remote port 1223 57--CREDITS-- 58Falko Menge <mail at falko-menge dot de> 59PHP Testfest Berlin 2009-05-09 60