1

I'm using Ubuntu 12.04.

I have a service that continuously sends data (binary images) to 127.255.255.255 in order to permit every future local service to get that data if needed.

is it possible to get that UDP data traffic via php in order to show it to the user whenever he/she is going to connect to the web server installed on the pc?

I already tried to create a socket in php and get data (simple data just to prove communication) and it works if I run the php code via command line, but if I visit the web page via browser I cannot see anything and I can see the web page that continuously load data until the moment it stops and show the data. This means that the php page can get data but it is going to show it only when the application stops.

I would like that when I visit that web page, I could see data streaming and only when I change web page the application stops.

The application seems to run for 30 seconds and memorize all the output and after that will write the output on the screen. 30 seconds are not setted by me, the app should run forever because it is into while(true) loop.

this is the code:

<body style="background-color: yellow;">
    <div style="margin: auto; margin-top: 50px;">
            <div style="margin: auto; width: 200px; font-weight: bold; font-size: 20px; margin-bottom: 20px; color: darkgreen;">Real Time View</div>

            <?php
                //Create a UDP socket
                if(!($socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP)))
                {
                     $errorcode = socket_last_error();
                     $errormsg = socket_strerror($errorcode);

                     die("Couldn't create socket: [$errorcode] $errormsg \n");
                }

                //echo "Socket created <br>";

                if( !socket_set_nonblock($socket) ){
                     $errorcode = socket_last_error();
                     $errormsg = socket_strerror($errorcode);

                     die("Could not nonblock socket : [$errorcode] $errormsg \n");
                }   

                // Bind the source address
                if( !socket_bind($socket, "0.0.0.0" , 9999) ){
                     $errorcode = socket_last_error();
                     $errormsg = socket_strerror($errorcode);

                     die("Could not bind socket : [$errorcode] $errormsg \n");
                }   
                //echo "Socket bind OK <br>";

                while(1){
                     //echo "Waiting for data ... <br>";

                     //Receive some data
                     //$r = socket_recvfrom($socket, $buf, 512, 0, $remote_ip, $remote_port);
                     //echo "$remote_ip : $remote_port -- " . $buf ."<br>";

                     $r = socket_read($socket, 1024);
                     echo $r; 

                     //Send back the data to the client
                     //socket_sendto($socket, "OK " . $buf , 100 , 0 , $remote_ip , $remote_port);
                }
                socket_close($socket);


            ?>
    </div>
</body>

any idea?

2 Answers 2

0

You could take a look at the flush() function

"Flushes the write buffers of PHP and whatever backend PHP is using (CGI, a web server, etc). This attempts to push current output all the way to the browser with a few caveats."

You probably also have to call ob_flush; So if you put the following on strategic places it should work:

echo "something";
flush();
ob_flush();

You can let PHP run infinite with set_time_limit(0). See the PHP Manual. So put this line on top of your php file.

Also check out the default_socket_timeout setting if you have problems after 60 seconds.

Edit: Running the script here now for 30 minutes without a problem. Even httpd cpu is low now. (due to the usleep i think)

My working code:

<?
  error_reporting(E_ALL);
  ini_set('display_errors', '1');
  set_time_limit(0);
  ob_start();

  function flush_buffers(){
      ob_end_flush();
      ob_flush();
      flush();
      ob_start();
  }
  echo date("d-m-Y H:i:s");

?>
<body style="background-color: yellow;">
    <div style="margin: auto; margin-top: 50px;">
            <div style="margin: auto; width: 200px; font-weight: bold; font-size: 20px; margin-bottom: 20px; color: darkgreen;">Real Time View</div>

            <?php
                //Create a UDP socket
                if(!($socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP)))
                {
                     $errorcode = socket_last_error();
                     $errormsg = socket_strerror($errorcode);
                     die("Couldn't create socket: [$errorcode] $errormsg \n");
                }

                echo "Socket created <br>";
                flush_buffers();

                if( !socket_set_nonblock($socket) ){
                     $errorcode = socket_last_error();
                     $errormsg = socket_strerror($errorcode);
                     die("Could not nonblock socket : [$errorcode] $errormsg \n");
                }

                // Bind the source address
                if( !socket_bind($socket, "0.0.0.0" , 9999) ){
                     $errorcode = socket_last_error();
                     $errormsg = socket_strerror($errorcode);
                     die("Could not bind socket : [$errorcode] $errormsg \n");
                }
                echo "Socket bind OK <br>";
                flush_buffers();

                socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array("sec"=>1, "usec"=>100));
                socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, array("sec"=>1, "usec"=>100));

                try {
                  while(1){
                       usleep(0.5 * 1000000); // 0.5 seconds
                       if (False === ($r = socket_read($socket, 1024))) {
                          $errorcode = socket_last_error();
                          $errormsg = socket_strerror($errorcode);
                          if ($errorcode!='11') { echo "error reading : [$errorcode] $errormsg <br>\n"; }
                       }
                       echo $r;
                       if ($r!='') { echo ".<br>"; }
                       flush_buffers();
                  }
                } finally {
                  socket_close($socket);
                }


            ?>
    </div>
</body>
<?
ob_end_flush();
?>
17
  • thank you for the answer. flush doesn't work. It seems that using it nothing changes. It seems that the application memorize all the output and when it stops running the output will be written. Another problem is that the application stops to run after 30 seconds without any reason
    – Gappa
    Commented Jul 12, 2013 at 10:46
  • The default for limit on running php script is 30 seconds. You can change that with set_time_limit (int $seconds). I'll take a look at your source.
    – Rik
    Commented Jul 12, 2013 at 11:05
  • How does this answer the OP's broader question?
    – user1345223
    Commented Jul 12, 2013 at 11:08
  • @GlenH7: What broader question? His original question was different. He had a problem whith flushing output before the PHP script was ready.
    – Rik
    Commented Jul 12, 2013 at 11:16
  • Yes, the OP could have done a better job at being more clear. It's in the title and the 5th paragraph. I would like that when I visit that web page, I could see data streaming and only when I change web page the application stops.
    – user1345223
    Commented Jul 12, 2013 at 11:19
-1

As already mentioned by the answers, the PHP script has a limited of execution time. In the default case this will be 30 seconds. In your script, there is a while loop to read the socket data. However, this makes the script runs infinity that is not expected by the server. The browser read the data in until the script is killed by the server.

You do need to this differently. The browser is not expecting a live stream in this case. You could do this with PHP and send the correct headers to the browser: stream/mjpg? for example. Than it should be able to show the image stream. Another possibility is to use javascript sockets to read the data and outputs it to the browser elements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.