fread() question

This is a discussion on fread() question within the PHP General forums, part of the PHP Programming Forums category; I have an open socket that I am reading from. The code looks like the following: $contentLength = 0 + $this->...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 07-11-2003
Seairth Jacobs
 
Posts: n/a
Default fread() question

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


Reply With Quote
  #2 (permalink)  
Old 07-12-2003
Simon Fredriksson
 
Posts: n/a
Default Re: fread() question

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
> seairth@seairth.com
>
>


Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 07:27 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0