Wouldn't it be easier to use something like the following?

while (!feof ($handle)) {
    $buffer = fgets($handle, 4096);
    echo $buffer;
}

This will read until there's nothing more to read. Isn't that what you're trying to do?

Read more about it at http://se2.php.net/manual/en/function.fgets.php

//Simon

Seairth Jacobs wrote:
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
[EMAIL PROTECTED]




--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to