刚刚从我拥有的一些工作代码中提取了这个片段
if (!socket_bind($sh, LISTENIP, LISTENPORT)) exit("Could not bind to socket");
while (TRUE) {
// $z = socket_recvfrom($sh, $data, 65535, 0, $connectip, $connectPort);
while(socket_recvfrom($sh, $data, 65535, 0, $connectip, $connectPort)) {
$pid = pcntl_fork();
if ($pid == -1) {
die('could not fork');
} else { #START ELSE COULD FORK
$PIDS[$pid] = $pid; //KEEP TRACK OF SPAWNED PIDS
if ($pid) {
//PARENT THREAD : $ch is a copy that we don't need in this thread
} else {
/** CHILD THREAD::BEGIN PROCESSING THE CONNECTION HERE! **/
include "include/child_thread.inc.php";
} //Child Thread
}//if-else-forked
/** CLEANUP THE CHILD PIDs HERE :: "Any system resources used by the child are freed." **/
foreach ($PIDS as $pid) pcntl_waitpid($pid,$status,WNOHANG);
$i++; //INCREASE CONNECTION COUNTER
}//While socket_accept
/** CLEANUP THE PARENT PIDS **/
foreach ($PIDS as $pid) {
$returnPid = pcntl_waitpid($pid,$status);
unset($PIDS[$pid]);
}
}//While True