11
votes

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;
1
Did you try to debug into TIdTCPConnection.Disconnect in IdTCPConnection.pas? Maybe a OnDisconnect handler is executed and uses the connection. Or the NotifyPeer plays a role, see also the comments in the source about different scenariosmjn
Freeing the object and creating a fresh new one will help (but not help to find the reason for the exception)mjn
I am not so much concerned with why I am getting an exception when calling disconnect. I just want to know how to properly recover from a situation like this.Andy Clark
This is hard to say without knowing what the program doesmjn
This is what I consider to be relevant (so feel free to ask for more). The client is our GUI, which issues commands to a server within our system. The client should recognize when the connection has failed and attempt to reconnect until the connection has been re-established. I have it working exactly as I want now. When the server terminates correctly, it calls DisconnectNotifyPeers, and the connections are closed cleanly. When the server terminates abnormally, the clients will recognize this and act appropriately. It is the code that is the problem. I'll post it below.Andy Clark

1 Answers

11
votes

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;