fread() question
| From: | Seairth Jacobs | Date: | Fri, 11 Jul 2003 15:59:25 +0000 |
| Subject: | fread() question | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-154906@lists.php.net to get a copy of this message | ||
I have an open socket that I am reading from. The code looks like the
following:
$contentLength = 0 + $this->response['headers']['content-length'];
do{
$status = socket_get_status($this->socket);
if( $status['eof'] == 1 ) {
if( $this->clientOptions['debug'] & DBGSOCK ) echo("DBG.SOCK status
eof met, finished socket_read\n");
break;
}
if($status['unread_bytes'] > 0) {
if($contentLength > $status['unread_bytes']) {
echo("DBG.SOCK reading {$status['unread_bytes']} bytes...\n");
$buffer = @fread($this->socket, $status['unread_bytes']);
$contentLength = $contentLength - $status['unread_bytes'];
} else {
$buffer = @fread($this->socket, $contentLength);
$contentLength = 0;
}
} else {
if( $this->clientOptions['debug'] & DBGSOCK ) echo("DBG.SOCK reading
{$status['unread_bytes']} bytes...\n");
usleep(1);
continue;
}
$data .= $buffer;
} while($contentLength > 0);
Now, the above code hangs (infinite loop, actually), ultimately returning
something like:
DBG.SOCK reading 3993 bytes...
DBG.SOCK reading 0 bytes...
DBG.SOCK reading 0 bytes...
DBG.SOCK reading 0 bytes...
DBG.SOCK reading 0 bytes...
DBG.SOCK reading 0 bytes...
DBG.SOCK reading 0 bytes...
DBG.SOCK reading 0 bytes...
DBG.SOCK reading 0 bytes...
etc...
On the other hand, if I change the following lines:
$buffer = @fread($this->socket, $status['unread_bytes'] - 1);
$contentLength = $contentLength - $status['unread_bytes'] - 1;
Then I get something that looks like:
DBG.SOCK reading 3993 bytes...
DBG.SOCK reading 1 bytes...
DBG.SOCK reading 1 bytes...
DBG.SOCK reading 1 bytes...
DBG.SOCK reading 1 bytes...
DBG.SOCK reading 1 bytes...
DBG.SOCK reading 1 bytes...
DBG.SOCK reading 1 bytes...
DBG.SOCK reading 1 bytes...
etc...
until the entire response has been read (88771 bytes in this case). Also,
this would mean that the effective fread would be:
$buffer = @fread($this->socket, 0);
However, if I just do:
$data = @fread($this->socket, $contentLength);
it works as expected. So what's going on?
For now, I will use the single command in place of the loop, but I really
wanted to monitor the current unread bytes. Any thoughts appreciated.
---
Seairth Jacobs
seairth@seairth.com