#23258 [Opn]: fgets() hangs 15 seconds on EOF of a socket

From: Date: Thu, 17 Apr 2003 14:13:59 +0000
Subject: #23258 [Opn]: fgets() hangs 15 seconds on EOF of a socket
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-37811@lists.php.net to get a copy of this message
ID: 23258 User updated by: markus dot pfefferle at web dot de Reported By: markus dot pfefferle at web dot de Status: Open Bug Type: Sockets related Operating System: Windows 2000 PHP Version: 4.3.1 New Comment: The "Array" is a mistake actually - an output from another place of my code. Yet still the mystery remains, why the loop goes through another cycle after the last call to fgets() has returned false and feof() is not reflecting the end of the connection. And why $r in this case does not check out as "false" in an expression to preliminary end the loop. Previous Comments: ------------------------------------------------------------------------ [2003-04-17 07:35:51] markus dot pfefferle at web dot de This well-known fragment of code takes up about 15 seconds on my machine: <pre> $fp = fsockopen($host, $port); fputs($fp, "POST $path HTTP/1.1\n"); fputs($fp, "Host: $host\n"); fputs($fp, "Content-type: application/x-www-form-urlencoded\n"); fputs($fp, "Content-length: ". strlen($data_to_send) ."\n"); fputs($fp, "Connection: close\n\n"); fputs($fp, "$data_to_send\n"); while(!feof($fp)) { $res .= fgets($fp, 128); } </pre> In order to trace the cause fot this pause, I modified whie while-loop to give me some debugging information: <pre> while(!feof($fp)) { $r = fgets($fp, 128); echo time()." : ".var_dump($r),"\n"; } </pre> Truns out this on the final 3 lines: <pre> 1050581993 : string(2) " " 1050581993 : bool(false) 1050582009 : Array </pre> The second line that returned false should already have flipped the EOF flag on the connection, but apparently it didn't. The last call that finally flips this flag however takes up 15 seconds of time and returns (odd enough) an empty array. Alrighty I think - if the false-value of the last chunk will give me a clue to break the loop, I just change my code to this: <pre> do { $r = fgets($fp, 128); echo time()." : ".var_dump($r),"\n"; } while ($r); </pre> And guess what - exactly the same 15 seconds, exactly the same output as before. The loop didn't even exit when $r was bool(false) according to var_dump(). This is what really puzzles me. I also used fread and toyed around with any possible length-parameter. It's always these 15 seconds delay at the last read that returns this odd empty array. ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=23258&edit=1

« previous php.bugs (#37811) next »