1--TEST--
2fgets() over a socket with more than a buffer's worth of data
3--CREDITS--
4Dave Kelsey <d_kelsey@uk.ibm.com>
5--FILE--
6<?php
7
8// create a file
9$filename = __FILE__ . ".tmp";
10$fd = fopen($filename, "w+");
11
12// populate the file with lines of data
13define("LINE_OF_DATA", "12345678\n");
14for ($i = 0; $i < 1000; $i++) {
15    fwrite($fd, LINE_OF_DATA);
16}
17fclose($fd);
18
19for ($i=0; $i<100; $i++) {
20  $port = rand(10000, 65000);
21  /* Setup socket server */
22  $server = @stream_socket_server("tcp://127.0.0.1:$port");
23  if ($server) {
24    break;
25  }
26}
27
28/* Connect to it */
29$client = fsockopen("tcp://127.0.0.1:$port");
30
31if (!$client) {
32    die("Unable to create socket");
33}
34
35/* Accept that connection */
36$socket = stream_socket_accept($server);
37
38echo "Write data from the file:\n";
39$data = file_get_contents($filename);
40unlink($filename);
41
42var_dump(fwrite($socket, $data));
43fclose($socket);
44
45echo "\nRead lines from the client\n";
46while ($line = fgets($client,256)) {
47    if (strcmp($line, LINE_OF_DATA) != 0) {
48        echo "Error - $line does not match " . LINE_OF_DATA;
49        break;
50    }
51}
52
53echo "\nClose the server side socket and read the remaining data from the client\n";
54fclose($server);
55while(!feof($client)) {
56    fread($client, 1);
57}
58
59echo "done\n";
60
61?>
62--EXPECT--
63Write data from the file:
64int(9000)
65
66Read lines from the client
67
68Close the server side socket and read the remaining data from the client
69done
70