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