1--TEST-- 2fgets() with a socket stream 3--CREDITS-- 4Dave Kelsey <d_kelsey@uk.ibm.com> 5--FILE-- 6<?php 7 8for ($i=0; $i<100; $i++) { 9 $port = rand(10000, 65000); 10 /* Setup socket server */ 11 $server = @stream_socket_server("tcp://127.0.0.1:$port"); 12 if ($server) { 13 break; 14 } 15} 16 17/* Connect to it */ 18$client = fsockopen("tcp://127.0.0.1:$port"); 19 20if (!$client) { 21 die("Unable to create socket"); 22} 23 24/* Accept that connection */ 25$socket = stream_socket_accept($server); 26 27echo "Write some data:\n"; 28fwrite($socket, "line1\nline2\nline3\n"); 29 30 31echo "\n\nRead a line from the client:\n"; 32var_dump(fgets($client)); 33 34echo "\n\nRead another line from the client:\n"; 35var_dump(fgets($client)); 36 37echo "\n\nClose the server side socket and read the remaining data from the client\n"; 38fclose($socket); 39fclose($server); 40while(!feof($client)) { 41 fread($client, 1); 42} 43 44echo "done\n"; 45 46?> 47--EXPECT-- 48Write some data: 49 50 51Read a line from the client: 52string(6) "line1 53" 54 55 56Read another line from the client: 57string(6) "line2 58" 59 60 61Close the server side socket and read the remaining data from the client 62done 63