|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2005-07-04 21:04 UTC] shaun at verticalevolution dot com
Description:
------------
Once the FEOF flag has been triggered on a stream you are unable to then write to that stream.
I created a stream_socket_server, accepted a client (stream_socket_accept) then attempted to read all the contents from the stream (while!feof()) I am not able to then write to that same client stream.
It appears that once the feof flag has been it the stream is no longer able to be written to.
Reproduce code:
---------------
SERVER:
<?php
$sock = stream_socket_server('tcp://127.0.0.1:1899', $errno, $errstr);
$read = Array($sock);
$i = '';
while(1){
$ready = stream_select($read, $write = NULL, $except = NULL, 10);
$c = stream_socket_accept($sock);
var_dump(stream_get_meta_data($c));
while(!feof($c)){
$i .= fread($c, 12);
}
echo $i;
fwrite($c, 'testback');
break;
}
fclose($sock);
?>
CLIENT:
<?php
$output = '';
$sock = stream_socket_client('tcp://127.0.0.1:1899',$errcode, $errstr);
fwrite($sock, 'test');
stream_set_timeout($sock, 5);
while(!feof($sock)){
$output = fread($sock, 1024);
}
fclose($sock);
echo $output;
?>
Expected result:
----------------
On the server I should see "test" printed out then on the client I should see "testback" printed out.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Nov 08 14:00:01 2025 UTC |
I moved the read array into the while script, this was a mistake on my part. This little server script is just a test to try to simplify my over all object. New Server Script: <?php $sock = stream_socket_server('tcp://127.0.0.1:1899', $errno, $errstr); $i = ''; while(1){ $read = Array($sock); $ready = stream_select($read, $write = NULL, $except = NULL, 10); $c = stream_socket_accept($sock); $read[] = $c; var_dump(stream_get_meta_data($c)); while(!feof($c)){ $i .= fread($c, 12); } var_dump(stream_get_meta_data($c)); echo $i; fwrite($c, 'testback'); break; } fclose($sock); ?> I only reason I had the stream_set_timeout in the client script because if I did not then the client just hangs with out ever quiting. I did remove it as per your request and I let it run for about 2 minutes with it never coming back. New Client script: <?php $output = ''; $sock = stream_socket_client('tcp://127.0.0.1:1899',$errcode, $errstr); fwrite($sock, 'test'); while(!feof($sock)){ $output .= fread($sock, 1024); } fclose($sock); echo $output; ?> After this I still don't get the fread every coming back.I've taken a look at your slides and I've also tried the stream_get_contents instead of the feof. This still does not work. Instead once the client makes the call the server now uses 100% CPU usage and still hangs. Server code: <?php $sock = stream_socket_server('tcp://127.0.0.1:1899', $errno, $errstr); $i = ''; while(1){ $read = Array($sock); $ready = stream_select($read, $write = NULL, $except = NULL, 10); $c = stream_socket_accept($sock); $read[] = $c; var_dump(stream_get_meta_data($c)); $i = stream_get_contents($c, -1); echo $i; fwrite($c, 'testback'); break; } fclose($sock); ?> client code: <?php $output = ''; $sock = stream_socket_client('tcp://127.0.0.1:1899',$errcode, $errstr); fwrite($sock, 'test'); $output = stream_get_contents($sock, -1); fclose($sock); echo $output; ?>Your usage of stream_select() is still wrong. $read_master = array($sock); while (1) { $read = $read_master; stream_select($read...) $c = stream_socket_accept() $read_master[] = $c; }