xref: /PHP-7.2/ext/sockets/tests/ipv6loop.phpt (revision 17ccbeec)
1--TEST--
2IPv6 Loopback test
3--SKIPIF--
4<?php
5	if (!extension_loaded('sockets')) {
6		die('skip sockets extension not available.');
7	}
8	require 'ipv6_skipif.inc';
9?>
10--FILE--
11<?php
12	/* Setup socket server */
13	$server = socket_create(AF_INET6, SOCK_STREAM, getprotobyname('tcp'));
14	if (!$server) {
15		die('Unable to create AF_INET6 socket [server]');
16	}
17	$bound = false;
18	for($port = 31337; $port < 31357; ++$port) {
19		if (socket_bind($server, '::1', $port)) {
20			$bound = true;
21			break;
22		}
23	}
24	if (!$bound) {
25		die("Unable to bind to [::1]:$port");
26	}
27	if (!socket_listen($server, 2)) {
28		die('Unable to listen on socket');
29	}
30
31	/* Connect to it */
32	$client = socket_create(AF_INET6, SOCK_STREAM, getprotobyname('tcp'));
33	if (!$client) {
34		die('Unable to create AF_INET6 socket [client]');
35	}
36	if (!socket_connect($client, '::1', $port)) {
37		die('Unable to connect to server socket');
38	}
39
40	/* Accept that connection */
41	$socket = socket_accept($server);
42	if (!$socket) {
43		die('Unable to accept connection');
44	}
45
46	socket_write($client, "ABCdef123\n");
47
48	$data = socket_read($socket, 10, PHP_BINARY_READ);
49	var_dump($data);
50
51	socket_close($client);
52	socket_close($socket);
53	socket_close($server);
54?>
55--EXPECT--
56string(10) "ABCdef123
57"
58