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