SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,810
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    You do not have a termination condition for the while loop.

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

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,154
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)
    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
    PHP Code:
    while (!feof($sock)) 

  5. #5
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    while (!feof($sock))

    will be same as

    while(1)

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

  6. #6
    SitePoint Addict webaddictz's Avatar
    Join Date
    Feb 2006
    Location
    Netherlands
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by shivam0101 View Post
    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:

    PHP Code:
    $errstr $errno null;
    $sock=fsockopen'localhost'5000$errstr$errno30 );

    while( !
    feof$sock ) ) {
         echo 
    fgets$sock128 );
         
    flush( );

    Yes, I blog, too.

  7. #7
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?

  8. #8
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  9. #9
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no. sessions are not used.

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

  10. #10
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?

  11. #11
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •