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 socket_recvfrom($socket, $buf, 12, 0, $from, $port); // cause warning 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 $msg = "Ping!"; 25 $len = strlen($msg); 26 $bytes_sent = socket_sendto($socket, $msg, $len, 0, $address, 1223); 27 if ($bytes_sent == -1) { 28 die('An error occurred while sending to the socket'); 29 } else if ($bytes_sent != $len) { 30 die($bytes_sent . ' bytes have been sent instead of the ' . $len . ' bytes expected'); 31 } 32 33 $from = ""; 34 $port = 0; 35 socket_recvfrom($socket, $buf, 12, 0); // cause warning 36 socket_recvfrom($socket, $buf, 12, 0, $from); // cause warning 37 $bytes_received = socket_recvfrom($socket, $buf, 12, 0, $from, $port); 38 if ($bytes_received == -1) { 39 die('An error occurred while receiving from the socket'); 40 } else if ($bytes_received != $len) { 41 die($bytes_received . ' bytes have been received instead of the ' . $len . ' bytes expected'); 42 } 43 echo "Received $buf from remote address $from and remote port $port" . PHP_EOL; 44 45 socket_close($socket); 46--EXPECTF-- 47Warning: socket_recvfrom(): unable to recvfrom [%d]: %a in %s on line %d 48 49Warning: Wrong parameter count for socket_sendto() in %s on line %d 50 51Warning: socket_recvfrom() expects at least 5 parameters, 4 given in %s on line %d 52 53Warning: Wrong parameter count for socket_recvfrom() in %s on line %d 54Received Ping! from remote address 127.0.0.1 and remote port 1223 55--CREDITS-- 56Falko Menge <mail at falko-menge dot de> 57PHP Testfest Berlin 2009-05-09 58