PHP socket_recvfrom 免费资源

PHP socket_recvfrom free resources

我有一个用 PHP 编写的 UDP 守护进程从远程 UDP 设备接收数据。

$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
}

有没有办法在每 x 次迭代后刷新缓冲区,因为它似乎在某一点填满并且数据库功能似乎不再运行,直到我终止并重新启动应用程序?

数据并不重要(这就是我使用 UDP 的原因)

使用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
}

有时,您可能还需要使用 flush()。此处解释的原因: