Return to fgets_socket_variation1.phpt CVS log | Up to [ELWIX - Embedded LightWeight unIX -] / embedaddon / php / ext / standard / tests / file |
1.1 misho 1: --TEST-- 2: fgets() with a socket stream 3: --CREDITS-- 4: Dave Kelsey <d_kelsey@uk.ibm.com> 5: --FILE-- 6: <?php 7: 1.1.1.2 ! misho 8: for ($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: } 1.1 misho 16: 17: /* Connect to it */ 1.1.1.2 ! misho 18: $client = fsockopen("tcp://127.0.0.1:$port"); 1.1 misho 19: 20: if (!$client) { 21: die("Unable to create socket"); 22: } 23: 24: /* Accept that connection */ 25: $socket = stream_socket_accept($server); 26: 27: echo "Write some data:\n"; 28: fwrite($socket, "line1\nline2\nline3\n"); 29: 30: 31: echo "\n\nRead a line from the client:\n"; 32: var_dump(fgets($client)); 33: 34: echo "\n\nRead another line from the client:\n"; 35: var_dump(fgets($client)); 36: 37: echo "\n\nClose the server side socket and read the remaining data from the client\n"; 38: fclose($socket); 39: fclose($server); 40: while(!feof($client)) { 41: fread($client, 1); 42: } 43: 44: echo "done\n"; 45: 46: ?> 47: --EXPECT-- 48: Write some data: 49: 50: 51: Read a line from the client: 52: string(6) "line1 53: " 54: 55: 56: Read another line from the client: 57: string(6) "line2 58: " 59: 60: 61: Close the server side socket and read the remaining data from the client 62: done