xref: /PHP-8.1/ext/sockets/tests/ipv4loop.phpt (revision 74859783)
1--TEST--
2IPv4 Loopback test
3--EXTENSIONS--
4sockets
5--FILE--
6<?php
7    /* Setup socket server */
8    $server = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
9    if (!$server) {
10        die('Unable to create AF_INET socket [server]');
11    }
12
13    if (!socket_bind($server, '127.0.0.1', 0)) {
14        die("Unable to bind to 127.0.0.1");
15    }
16
17    if (!socket_listen($server, 2)) {
18        die('Unable to listen on socket');
19    }
20
21    socket_getsockname($server, $unused, $port);
22
23    /* Connect to it */
24    $client = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
25    if (!$client) {
26        die('Unable to create AF_INET socket [client]');
27    }
28    if (!socket_connect($client, '127.0.0.1', $port)) {
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