1--TEST-- 2ext/sockets - socket_getpeername_ipv4loop - basic test 3--CREDITS-- 4Tatjana Andersen tatjana.andersen@redpill-linpro.com 5# TestFest 2009 - NorwayUG 6--EXTENSIONS-- 7sockets 8--FILE-- 9<?php 10 /* Bind and connect sockets to localhost */ 11 $localhost = '127.0.0.1'; 12 13 /* Setup socket server */ 14 $server = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); 15 if (!$server) { 16 die('Unable to create AF_INET socket [server]'); 17 } 18 19 $minport = 31337; 20 $maxport = 31356; 21 $bound = false; 22 for($port = $minport; $port <= $maxport; ++$port) { 23 if (@socket_bind($server, $localhost, $port)) { 24 $bound = true; 25 break; 26 } 27 } 28 if (!$bound) { 29 die('Unable to bind to '.$localhost); 30 } 31 if (!socket_listen($server, 2)) { 32 die('Unable to listen on socket'); 33 } 34 35 /* Connect to it */ 36 $client = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); 37 if (!$client) { 38 die('Unable to create AF_INET socket [client]'); 39 } 40 if (!socket_connect($client, $localhost, $port)) { 41 die('Unable to connect to server socket'); 42 } 43 44 /* Accept that connection */ 45 $socket = socket_accept($server); 46 if (!$socket) { 47 die('Unable to accept connection'); 48 } 49 50 if (!socket_getpeername($client, $address, $peerport)) { 51 die('Unable to retrieve peer name'); 52 } 53 var_dump($address, $port === $peerport); 54 55 socket_close($client); 56 socket_close($socket); 57 socket_close($server); 58?> 59--EXPECT-- 60string(9) "127.0.0.1" 61bool(true) 62