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
19/* Setup socket server */
20$server = stream_socket_server('tcp://127.0.0.1:31337');
21
22/* Connect to it */
23$client = fsockopen('tcp://127.0.0.1:31337');
24
25if (!$client) {
26	die("Unable to create socket");
27}
28
29/* Accept that connection */
30$socket = stream_socket_accept($server);
31
32echo "Write data from the file:\n";
33$data = file_get_contents($filename);
34unlink($filename);
35
36var_dump(fwrite($socket, $data));
37fclose($socket);
38
39echo "\nRead lines from the client\n";
40while ($line = fgets($client,256)) {
41	if (strcmp($line, LINE_OF_DATA) != 0) {
42		echo "Error - $line does not match " . LINE_OF_DATA;
43		break;
44	}
45}
46
47echo "\nClose the server side socket and read the remaining data from the client\n";
48fclose($server);
49while(!feof($client)) {
50	fread($client, 1);
51}
52
53echo "done\n";
54
55?>
56--EXPECT--
57Write data from the file:
58int(9000)
59
60Read lines from the client
61
62Close the server side socket and read the remaining data from the client
63done
64