1

I have a UDP daemon written in PHP receiving data from remote UDP devices.

$sock = socket_create(AF_INET, SOCK_DGRAM, 0);
socket_bind($sock, 0, $port) or die('Could not bind to address'); 

while (true) {
  $r = socket_recvfrom($sock, $buf, 65535, 0, $remote_ip, $remote_port);
  echo "$remote_ip : $remote_port -- " . $buf ."\n";
  echo strlen($buf) . "\n";

  // DO DATABASE FUNCTIONS
}

Is there a way to flush the buffer after every x amount of iterations as it seems that at a certain point it fills up and the database function doesn't seem to operate anymore until I kill and restart the application?

Data is not critical (that's why I'm using UDP)

Fabrizio Mazzoni
  • 1,831
  • 2
  • 24
  • 46

1 Answers1

1

Use ob_flush()

$sock = socket_create(AF_INET, SOCK_DGRAM, 0);
socket_bind($sock, 0, $port) or die('Could not bind to address'); 

while (true) {
  $r = socket_recvfrom($sock, $buf, 65535, 0, $remote_ip, $remote_port);
  echo "$remote_ip : $remote_port -- " . $buf ."\n";
  echo strlen($buf) . "\n";

  if(strlen($buf)%1024 == 0)
  {
      flush();
      ob_flush();
  } 

  // DO DATABASE FUNCTIONS
}

Sometimes, you may need to use flush() also. Reason explained here : https://stackoverflow.com/a/4191417/1218075

Community
  • 1
  • 1
Makesh
  • 1,236
  • 1
  • 11
  • 25
  • Is it ok flushing at multiples of 1024 even though my buffer is 65535? – Fabrizio Mazzoni May 25 '15 at 12:42
  • the 3rd arg in socket_recvfrom() indicates the maximum lenght `$buf` can hold. Assume, you are recieving only 10000 bytes, then flush() will never occur. Its just an example. Its upto you how you implement, it can be either counter based or bytes based flush. – Makesh May 26 '15 at 12:18