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