I have the following code for listening socket for client connections
<?php
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$port = "1234";
socket_bind($socket, '192.168.1.1', $port);
socket_listen($socket);
socket_set_nonblock($socket);
while (true) {
if (($newConnection = socket_accept($socket)) !== false) {
$childProcessID = pcntl_fork();
if ($childProcessID == -1) {
throw new Exception('Could not fork');
} else if (!$childProcessID) {
$myProcessID = getmypid();
$lastActive = time();
while (true) {
if (socket_recv($newConnection, $currentMsg, 2045, MSG_DONTWAIT) >= 1) {
// Process Message
}
}
}
?>
There’s no syntax issue or functionality wise in the code except when I run it, due to the while loop, it’s taking most of the CPU cycles. Is there a better way of listening the socket connection?
5