0

I'm writing a NAT traversal node in PHP and I would like to be able to send data before the connection is closed. I need the script to send a string before blocking while waiting for data on a socket. It seems that echo writes to a buffer that is flushed and sent in bulk when the script exits or die is called. This is my current code:

$sktSocket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
$bSuccess = socket_bind($sktSocket, $_SERVER['SERVER_ADDR'], $sPass);
if($bSuccess){
    echo "wait for connection";
    flush();
    ob_flush();
    $remote_ip = "";
    $remote_port = "";
    $r = socket_recvfrom($sktSocket, $buf, 512, MSG_WAITALL, $remote_ip, $remote_port);
    //socket_sendto($sktSocket, "OK " . $buf , 100 , 0 , $remote_ip , $remote_port);
    socket_close($sktSocket);
}

I've tried flush, and ob_flush, but those do not do what I need.

Is there a way in php to write to the output stream before the blocking call (socket_recvfrom) so that the data is sent and then it blocks?

TheBat
  • 1,006
  • 7
  • 25

0 Answers0