recovering from "Connection Reset By Peer" Indy TCP Client

Andy Clark picture Andy Clark · May 9, 2012 · Viewed 14.1k times · Source

How should I be recovering in this situation?

The server crashes, thus the connection has been abnormally closed. Calls to almost everything result in "Connection Reset By Peer" exceptions. I seem to have fixed it by calling Disconnect on the TIdTCPClient object inside the except block, but it results in one final exception with the same message (which I have caught in the second try-except block).

This is with Indy10 and Delphi XE2.

   try
      if not EcomSocket.Connected then EcomSocket.Connect();
    except
      on e: Exception do begin
        try
          EcomSocket.Disconnect();
        except
          MessageDlg('Connectivity to the server has been lost.', mtError, [mbOK], 0);
        end;
      end;
    end;

Answer

Remy Lebeau picture Remy Lebeau · May 9, 2012

Try this:

try 
  if not EcomSocket.Connected then EcomSocket.Connect(); 
except 
  try 
    EcomSocket.Disconnect(False); 
  except 
  end; 
  if EcomSocket.IOHandler <> nil then EcomSocket.IOHandler.InputBuffer.Clear; 
  MessageDlg('Connectivity to the server has been lost.', mtError, [mbOK], 0); 
end;