xref: /PHP-5.4/ext/sockets/tests/ipv4loop.phpt (revision d91bc9ec)
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	if (!socket_bind($server, '127.0.0.1', 31337)) {
17		die('Unable to bind to 127.0.0.1:31337');
18	}
19	if (!socket_listen($server, 2)) {
20		die('Unable to listen on socket');
21	}
22
23	/* Connect to it */
24	$client = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));
25	if (!$client) {
26		die('Unable to create AF_INET socket [client]');
27	}
28	if (!socket_connect($client, '127.0.0.1', 31337)) {
29		die('Unable to connect to server socket');
30	}
31
32	/* Accept that connection */
33	$socket = socket_accept($server);
34	if (!$socket) {
35		die('Unable to accept connection');
36	}
37
38	socket_write($client, "ABCdef123\n");
39
40	$data = socket_read($socket, 10, PHP_BINARY_READ);
41	var_dump($data);
42
43	socket_close($client);
44	socket_close($socket);
45	socket_close($server);
46?>
47--EXPECT--
48string(10) "ABCdef123
49"
50