php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #32688 stream_set_timeout does not work
Submitted: 2005-04-12 23:46 UTC Modified: 2005-04-14 10:46 UTC
From: rene dot vogt at cnlab dot ch Assigned:
Status: Not a bug Package: Sockets related
PHP Version: 5.0.4 OS: Windows 2003 Server
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: rene dot vogt at cnlab dot ch
New email:
PHP Version: OS:

 

 [2005-04-12 23:46 UTC] rene dot vogt at cnlab dot ch
Description:
------------
The function stream_set_timeout seems not to work on php 5.0.4 (win32). It worked with earlier php versions.
The sample code connects to a page on a server which prints out a dot every second. After 3 seconds the script should terminate.

http://verkehr.cnlab.ch/test1.php (sample code) 
http://verkehr.cnlab.ch/test2.php (produces dots)

Reproduce code:
---------------
<?php
 $fp = fsockopen("verkehr.cnlab.ch", 80, $errno, $errstr, 3);
 if (!$fp) {
   return "Error connecting";
 } else {
   stream_set_timeout($fp, 3);
   fwrite($fp,"GET /test2.php HTTP/1.0\r\nHost: verkehr.cnlab.ch:80\r\nConnection: Close\r\n\r\n");
   while (!feof($fp)) {
     echo fread($fp, 512);flush(); ob_flush();
   }
   fclose($fp);
}
?>

Expected result:
----------------
Prints out 3 dots and abort


Actual result:
--------------
Print out dots until page timeout

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-04-13 17:21 UTC] tony2001@php.net
With what version it worked before?
I doubt it could work at all in the way you describe, because in this situation there is _no timeout_, it's quite expected situation: you open a stream and read the data from it, so why should it timeout? 
 [2005-04-14 10:29 UTC] rene dot vogt at cnlab dot ch
I found the solution, sorry this is NO php bug. 
I have to check how many bytes I get by the fread function. If this returns 0 bytes a timeout has occurd. I though I would get an eof but this is not the case. 
You can close this ticket.

The correct code should look like this:
<?php
 $fp = fsockopen("verkehr.cnlab.ch", 80, $errno, $errstr, 3);
 if (!$fp) {
   return "Error connecting";
 } else {
   stream_set_timeout($fp, 3);
   fwrite($fp,"GET /test2.php HTTP/1.0\r\nHost:
verkehr.cnlab.ch:80\r\nConnection: Close\r\n\r\n");
   $bufferlen=-1;
   while (!feof($fp) && $bufferlen!=0) {
     $buffer = fread($fp, 512);
     $bufferlen=strlen($buffer);
     echo $buffer; flush(); ob_flush();
   }
   fclose($fp);
}
?>
 [2005-04-14 10:46 UTC] tony2001@php.net
No bug -> bogus.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Jan 11 13:01:30 2025 UTC