PHP Socket Disconnecting Randomly
<p>I'm using a PHP Socket to listen on port 6000 for incoming connections and its working perfectly 99% percent of the time but 1% of the time the client is getting a connection error when sending a request to the server. I created a different script to ping the socket on port 6000 every second in an infinite loop and write the result to a log file so I can see if its breaking, and out of 78,000 Successful pings, 23 Failed. </p>

<p>There must be some small logic error with my code which is causing this. If anyone has any ideas its much appreciated.</p>

<p>Socket:</p>

<pre><code>if(!($sock = socket_create(AF_INET, SOCK_STREAM, 0)))
{
    $errorcode = socket_last_error();
    $errormsg = socket_strerror($errorcode);

    die("Couldn't create socket: [$errorcode] $errormsg \n");
}

echo "Socket created \n";

// Bind the source address
if( !socket_bind($sock, "0.0.0.0" , 6000) )
{
    $errorcode = socket_last_error();
    $errormsg = socket_strerror($errorcode);

    die("Could not bind socket : [$errorcode] $errormsg \n");
}

echo "Socket bind OK \n";

if(!socket_listen ($sock , 10))
{
    $errorcode = socket_last_error();
    $errormsg = socket_strerror($errorcode);

    die("Could not listen on socket : [$errorcode] $errormsg \n");
}

echo "Socket listen OK \n";

echo "Waiting for incoming connections... \n";

//start loop to listen for incoming connections
while (true)
{
    //Accept incoming connection - This is a blocking call
    $client =  socket_accept($sock);

    //read data from the incoming socket
    $input = "";
    $input = socket_read($client, 10000000);

     if ($input != "")
     {
        // do my logic here with $input
     }
}
socket_close($sock);
</code></pre>

<p>EDIT: No, I'm not using CMD to ping. This is my PHP script which is doing the pinging:</p>

<pre><code>&lt;?php
$host = '0.0.0.0';
$port = 6000;
$waitTimeoutInSeconds = 1;
while(true)
{
   if($fp = fsockopen($host,$port,$errCode,$errStr,$waitTimeoutInSeconds))
   {   
       $file = 'log.txt';
       $current = file_get_contents($file);
       $today = date("Y-m-d_H:i:s");  
       $current .= $today . " - SUCCESS\n";
       file_put_contents($file, $current);
   } 
   else 
   {
       $file = 'log.txt';
       $current = file_get_contents($file);
       $today = date("Y-m-d_H:i:s");  
       $current .= $today . " - FAILED\n";
       file_put_contents($file, $current);
   } 
   fclose($fp);
   sleep(1);
}
?&gt;
</code></pre>

<p>For an actual transaction, the client is only connected for a split second while it sends through an xml request in raw text, then it does some logic which takes less than a second. Since its failing on the ping test though, that means my listener is breaking for a second for one reason or another does it not?</p>
