title: how to get UDP traffic via PHP and show it in a web page?
body: <p>I'm using Ubuntu 12.04. </p>
  
  <p>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.</p>
  
  <p>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?</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>this is the code:</p>
  
  <pre><code>&lt;body style="background-color: yellow;"&gt;
      &lt;div style="margin: auto; margin-top: 50px;"&gt;
              &lt;div style="margin: auto; width: 200px; font-weight: bold; font-size: 20px; margin-bottom: 20px; color: darkgreen;"&gt;Real Time View&lt;/div&gt;
  
              &lt;?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 &lt;br&gt;";
  
                  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 &lt;br&gt;";
  
                  while(1){
                       //echo "Waiting for data ... &lt;br&gt;";
  
                       //Receive some data
                       //$r = socket_recvfrom($socket, $buf, 512, 0, $remote_ip, $remote_port);
                       //echo "$remote_ip : $remote_port -- " . $buf ."&lt;br&gt;";
  
                       $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);
  
  
              ?&gt;
      &lt;/div&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p>any idea?</p>