PHP Socket Server CPU 100%

Hello,

I’m running a modified version of the script from

http://www.kirupa.com/developer/flash8/php5sockets_flash8.htm

I got everything to work even with Flash player 9 through the policy file setup.

I have a problem that occurs completely random. I run the chat.php file to run the socket server… and clients would connect and chat without a problem. However, I would come back later to find that the CPU is running at 100%. The output would say client disconnected over and over again freezing the whole server. I have no idea when and why this occurs. Its completely random.

Here is the code of the chat.php file


while (true) {

    $arrChangedSockets = $objController->getReadSockets();
    array_push($arrChangedSockets, $rsMaster);

    // socket_select is used here to see if there were any changes with the sockets that were connected before
    // $intNumChangedSockets is here so you can check to see if the change is true or not with a subsequent function
    $intNumChangedSockets = socket_select($arrChangedSockets, $arrChangedSocketsWrite = NULL, $arrChangedSocketsException = NULL, $intTimeout = NULL);


    // we run a foreach function on $arrChangedSockets to see whether it needs to be added (if new), determine the message it sent
    // or determine if there was a socket disconnect
    foreach($arrChangedSockets as $rsSocket) {

        // now if the $rsSocket currently being checked is the $rsMaster socket, we need to run some checks
        // if not then we will skip down to else and check the messages that were sent

        if ($rsSocket == $rsMaster) {

            // socket_accept will accept any incoming connections on $rsMaster, and if true, it will return a resource id
            // which we have set to $rsClient. If this did not work then return an error, but if it worked, then add in
            // $rsClient to our $arrReadSockets array at the end.
            if (($rsClient = socket_accept($rsMaster)) < 0) {
                echo "socket_accept() failed: reason: " . socket_strerror($msgsock) . "
<br />";
                continue;
               } else {
                $objController->addReadSocket($rsClient);
            }

            echo "$rsClient connected :)
";
            flush();
            
            if( $rsOldConnect == $rsSocket )
                continue;
            $rsOldConnect = $rsSocket;

       } else {

               // socket_recv just receives data from $rsSocket as $strBuffer, with an integer length of 2048, and a mystery flag set to 0
            // that mystery flag has no real documentation so setting it to 0 will solve it as others have done.
            // we've also set it as a var $intBytes in case we need to ensure data sending with socket_write, which is optional
            $intBytes = socket_recv($rsSocket, $strBuffer, $intLength = 2048, $intFlags = 0);

            // if the $intBytes we have is 0, then it is a disconnect message from the socket
            // we will just search for it as an index and unset that socket from our $arrReadSockets and
            // finish up with using socket_close to ensure it is closed off
            
            echo "Bytes Returned: x".$intBytes."x
";

            if ( $intBytes == 0 || strlen($strBuffer) == 0 || !$intBytes ) {
                echo "$rsSocket disconnected
";
                $objController->disconnectSocket($rsSocket);
                flush();
            } else{
                $objController->processRequest($rsSocket, $strBuffer);
            }

            if( $rsOldDisconnect == $rsSocket )
                continue;
            $rsOldDisconnect = $rsSocket;
        }
    }
}

I have a feeling its in the segment where socket_rcvd function is and where it checks the $intBytes, but I’m not sure.

Please help.

Thank You,
Armin