socket_getpeername_ipv4loop.phpt 1.6 KB

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