cannot read full data using fsockopen and fgets

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sergkr
Forum Newbie
Posts: 2
Joined: Thu May 10, 2007 8:34 am

cannot read full data using fsockopen and fgets

Post by sergkr »

Hi everybody!

I am not sure if my problem is configuration or programming issue.

I have a script that reads data from remote server via socket connection, something like this:

...
$fp = fsockopen($conn_host, $conn_port);

stream_set_blocking($fp,0);
while (!feof($fp) && !$stop) {
$line = fgets($fp, 1024);
echo($line);
flush();
} //while
stream_set_blocking($fp,1);
fclose($fp);

I put it on a web site and it worked fine for several days. Then it stopped working properly. Host provider assured me thy did no do any changes in configuration. They also restarted the server but that did not help.

I did some testing and see that the scripts works in following way:

1. It opens the socket connection successfully.
2. It reads some block of data
3. Then it hangs up for 30 sec and throws error
"Maximum execution time of 30 seconds exceeded in line XX" where line # is the line with fgets function.

Any idea why it this problem happen? It is really critical issue.

Thanks.
bubblenut
Forum Newbie
Posts: 20
Joined: Sat Feb 03, 2007 4:16 am
Location: London

Post by bubblenut »

Does the host you're connceting to automatically close the connection?
Why are you using non-blocking I/O if you're not doing anything with the cycles that would be blocked by I/O?
If there's no immediate need for non-blocking I/O you should not use it.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Sometimes on streams feof() always returns false. SMTP response for example do not "end" as PHP sees it. You have to check each line of the response and manually look for the last line yourself. I'm not sure if this will apply in your case but I'd be willing to guess feof() is returning false all the time.
Post Reply