1

My task is to write a full duplex TCP/IP client using PHP. The client connects to a server and sends a heartbeat packet using a predefined interval. This is OK and I know how to handle it. The server can send a data packet time to time. This is my problem - I don't know to "listen" for the incoming data and send an answer in parallel with the heartbeat function. This is my simplified code:

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($socket, $address, $port);

$lastHeartBeat = 0;

do {
   if(time() - $lastHeartBeat >= 60) {
       $binarydata = pack("c*", 1, 5, 0, 0, 0, 0, 0, 0 );

       socket_write($socket, $binarydata, strlen($binarydata));

       $resp = socket_read($socket, 2048);

       $lastHeartBeat = time();
   }
} while (true);
user3523426
  • 836
  • 3
  • 11
  • 26

0 Answers0