Browser does not load

Hello,

I have the following code which works fine for the first client, if i open another window then the browser is not loading completely, it keeps on loading.

ob_start();
$sock=fsockopen(‘localhost’,5000,$errstr,$errno,30);
while(1)
{
echo fgets($sock,128);
}
ob_end_flush();

Thanks
Shivam

You do not have a termination condition for the while loop.

while(1) {…} will circle forever unless there is an exit test inside.

But for the first client it works fine, when another client tries it keeps on loading.

I am trying to display data in the browser in realtime. The data are coming from socket server with 1 sec delay. Is there any php function/ way to implement it.

Thanks

If it happened to “work” my guess is that it was timing out and dying without your knowledge of the error. IMHO that’s horrendous coding as it is now.

When you say display “realtime” I’m confused. PHP runs and displays output, that output can’t be updated “live”. Maybe you’re looking for a javascript AJAX/widget type of solution?

If you mean “realtime” as in one-time when the PHP file runs, you can open a socket, but don’t let it loop infinately. Try using

while (!feof($sock))

while (!feof($sock))

will be same as

while(1)

since data from server will be continues with a delay of 1 second

Not precisely. The nett result will pretty much be the same, but the implementation differs. while( !feof( $sock ) ) actually says: when there is still information coming from the socket, do the following. while( 1 ) on the other hand says: do this for all eternity, infinity and beyond. Well, until the script is killed anyway. If you encounter an error from the socket, the first will kill the script, where the second will not, unless it’s fatal.

Your problem though, seems to be that you only flush after the infinite loop, which is supposed to never happen. Also, I see no reason for output buffering. This might work better:


$errstr = $errno = null;
$sock=fsockopen( 'localhost', 5000, $errstr, $errno, 30 );

while( !feof( $sock ) ) {
     echo fgets( $sock, 128 );
     flush( );
}

thanks.

Still the same problem. The first client does not have any problem. When i open 2 simultaneous clients the second client keeps on loading without any display.

I may be wrong in the way of doing. What i need is,
The data the socket server sends should be displayed in browser - (may be in graph later on). Is it possible to send from (3000 port - port iam using for socket) to 80 port?

Are you using sessions?

If yes, then the first request has a file lock on the sessions file, and so you must have the first request release that lock for other requests to access the session.

Also, if many people are going to be using this, then it’s time to roll your own server software (possibly based on an existing implementation) or find some existing software better fit for this.

no. sessions are not used.

The server simply sends data when a message is sent from the client.

Well, there’s no problem with the code presented, that’s for sure.

Is there any anything that is sent (and forcibly flushed) before that piece of code?

How long have you waited on the 2nd request before ‘giving up’ and closing your browser?

Do you have session auto-start on by chance?

Have you asked anyone else to try it simultaneously?

Ok leave that. For time being i will connect to one client.

I want to send these data to a webpage. How do i achieve this? any ideas?