1--TEST--
2stream_socket_shutdown() test on IPv4 TCP Loopback
3--SKIPIF--
4<?php
5    function_exists('stream_socket_shutdown') or die('skip stream_socket_shutdown() is not supported.');
6?>
7--FILE--
8<?php
9
10  for ($i=0; $i<100; $i++) {
11    $port = rand(10000, 65000);
12    /* Setup socket server */
13    $server = @stream_socket_server("tcp://127.0.0.1:$port");
14    if ($server) {
15      break;
16    }
17  }
18
19if (!$server) {
20        die('Unable to create AF_INET socket [server]');
21    }
22
23    /* Connect and send request 1 */
24    $client1 = stream_socket_client("tcp://127.0.0.1:$port");
25    if (!$client1) {
26        die('Unable to create AF_INET socket [client]');
27    }
28    @fwrite($client1, "Client 1\n");
29    stream_socket_shutdown($client1, STREAM_SHUT_WR);
30    @fwrite($client1, "Error 1\n");
31
32    /* Connect and send request 2 */
33    $client2 = stream_socket_client("tcp://127.0.0.1:$port");
34    if (!$client2) {
35        die('Unable to create AF_INET socket [client]');
36    }
37    @fwrite($client2, "Client 2\n");
38    stream_socket_shutdown($client2, STREAM_SHUT_WR);
39    @fwrite($client2, "Error 2\n");
40
41    /* Accept connection 1 */
42    $socket = stream_socket_accept($server);
43    if (!$socket) {
44        die('Unable to accept connection');
45    }
46    @fwrite($socket, fgets($socket));
47    @fwrite($socket, fgets($socket));
48    fclose($socket);
49
50    /* Read Response 1 */
51    echo fgets($client1);
52    echo fgets($client1);
53
54    /* Accept connection 2 */
55    $socket = stream_socket_accept($server);
56    if (!$socket) {
57        die('Unable to accept connection');
58    }
59    @fwrite($socket, fgets($socket));
60    @fwrite($socket, fgets($socket));
61    fclose($socket);
62
63    /* Read Response 2 */
64    echo fgets($client2);
65    echo fgets($client2);
66
67    fclose($client1);
68    fclose($client2);
69    fclose($server);
70?>
71--EXPECT--
72Client 1
73Client 2
74