Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

recovering from "Connection Reset By Peer" Indy TCP Client

Tags:

tcp

delphi

indy

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;
like image 820
Andy Clark Avatar asked May 09 '12 12:05

Andy Clark


1 Answers

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; 
like image 59
Remy Lebeau Avatar answered Sep 19 '22 09:09

Remy Lebeau