php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #33568 stream_socket_server can't write after feof triggered
Submitted: 2005-07-04 21:04 UTC Modified: 2005-07-05 23:02 UTC
From: shaun at verticalevolution dot com Assigned: wez (profile)
Status: Not a bug Package: Sockets related
PHP Version: 5CVS-2005-07-05 OS: Windows XP SP2
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: shaun at verticalevolution dot com
New email:
PHP Version: OS:

 

 [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.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-07-04 21:58 UTC] tony2001@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5-win32-latest.zip


 [2005-07-05 13:46 UTC] wez@php.net
Please also remove the stream_set_timeout() call from your script(s) and note that you are not using stream_select() correctly (the read/write/excep arrays need to be set on *each* call).
 [2005-07-05 14:22 UTC] shaun at verticalevolution dot com
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.
 [2005-07-05 14:33 UTC] shaun at verticalevolution dot com
Please note also that I've tried the new scripts with the CVS snapshot as well.

PHP Version: 5.1.0-dev
 [2005-07-05 16:10 UTC] wez@php.net
Take a look through these slides:
http://netevil.org/talks/index.php?t=lucky-dip

Pay particular attention to the part that talks about feof() and network streams.

(hint: use stream_get_contents())
 [2005-07-05 16:36 UTC] shaun at verticalevolution dot com
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;
?>
 [2005-07-05 18:59 UTC] wez@php.net
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;
}

 [2005-07-05 23:02 UTC] sniper@php.net
And this is not a support forum on HOW to use PHP.

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 17:01:29 2024 UTC