Howdy all,
I've got a PHP script talking to a (C++) server. The PHP script opens a socket, requests data, gets the data, then closes the socket. However I'm seeing huge amounts of "Connection reset by peer" in the PHP debug files. I close the socket and re-open it, when this happens, and I'm wondering if there's a better way of doing this??

socket_read() will also return true, but only a 1 byte char (NULL), which I've noticed, don't know why, which is why I check for that also.

This is how the PHP handles the socket:

PHP Code:

function GetSocketToRemora(&$socket)
{
   
$address "127.0.0.1"
   
$service_port "11428";
   
   
//close this socket if it's open
   
if (is_resource($socket))
   {
      
socket_close($socket);
   } 
//end if
   
   //now (re)create socket
   
$socket socket_create(AF_INETSOCK_STREAMSOL_TCP);
   if (
$socket === false)
   {
      
Logger("ERROR: socket_create() failed: " socket_strerror(socket_last_error($socket)), $debugFile);
   } 
//end if

   
$result socket_connect($socket$address$service_port);
   if (
$result === false)
   {
      
Logger("ERROR: socket_connect() failed: ($result) " socket_strerror(socket_last_error($socket)), $debugFile);
   } 
//end if
   
   
if (!is_resource($socket))
   {
      
Logger("ERROR: Could not create socket resource"$debugFile);
   } 
//end if
   
   
return $socket;

//end function GetSocketToRemora()


  //get socket resource and request data
   
$socket GetSocketToRemora($socket);
   while (
socket_write($socket$msgstrlen($msg)) === false)
   {
      
Logger("ERROR: Write failed: " socket_strerror(socket_last_error($socket)), $debugFile);
   } 
//end while
   
   //read data, and keep reading if it failed, or we only get a single byte in response
   
$data "";
   while (((
$data socket_read($socket512PHP_NORMAL_READ)) === false) || (strlen($data) == ))
   {
      
//if read failed, start a new socket and repeat the request....
      
if ($data === false)
      {
         
Logger("ERROR: read failed. Data:" $data "\nReason: (" socket_last_error($socket) . ") " socket_strerror(socket_last_error($socket)), $debugFile);  
      
         
//get socket resource
         
$socket GetSocketToRemora($socket);
         
         while (
socket_write($socket$msgstrlen($msg)) === false)
         {
            
Logger("ERROR: Write failed: " socket_strerror(socket_last_error($socket)), $debugFile);
         } 
//end while
           
      
//end if
      
else
      {
         
Logger("Only got a single byte in response, trying again..."$debugFile);
      } 
//end else
   
//end while read failed or data incomplete 

and this is what I'm seeing in $debugFile:


January 16 2004 at 13:17:41
ERROR: read failed. Data:
Reason: (104) Connection reset by peer
January 16 2004 at 13:17:41
ERROR: read failed. Data:
Reason: (104) Connection reset by peer
January 16 2004 at 13:17:41
ERROR: read failed. Data:
Reason: (104) Connection reset by peer
January 16 2004 at 13:17:41
ERROR: read failed. Data:
Reason: (104) Connection reset by peer
January 16 2004 at 13:17:41
ERROR: read failed. Data:
Reason: (104) Connection reset by peer
January 16 2004 at 13:17:41
ERROR: read failed. Data:
Reason: (104) Connection reset by peer
January 16 2004 at 13:17:41
ERROR: read failed. Data:
Reason: (104) Connection reset by peer
January 16 2004 at 13:17:41
ERROR: read failed. Data:
Reason: (104) Connection reset by peer




Is there a better way to handle "Connection reset by peer"?

Thanks for any help.