xref: /php-src/ext/sockets/tests/unixloop.phpt (revision 74859783)
1--TEST--
2Unix domain socket Loopback test
3--EXTENSIONS--
4sockets
5--SKIPIF--
6<?php
7if (substr(PHP_OS, 0, 3) == 'WIN') {
8    die('skip.. Not valid for Windows');
9}
10
11?>
12--FILE--
13<?php
14    $sock_path = sprintf("/tmp/%s.sock", uniqid());
15
16    if (file_exists($sock_path))
17        die('Temporary socket already exists.');
18
19    /* Setup socket server */
20    $server = socket_create(AF_UNIX, SOCK_STREAM, 0);
21    if (!$server) {
22        die('Unable to create AF_UNIX socket [server]');
23    }
24    if (!socket_bind($server,  $sock_path)) {
25        die("Unable to bind to $sock_path");
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_UNIX, SOCK_STREAM, 0);
33    if (!$client) {
34        die('Unable to create AF_UNIX socket [client]');
35    }
36    if (!socket_connect($client, $sock_path)) {
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    @unlink($sock_path);
55?>
56--EXPECT--
57string(10) "ABCdef123
58"
59