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