Search code examples
delphidelphi-2009indy

TIdTCPClient: Reconnect implementation


I am writing an application, which uses TIdTCPClient to communicated with another application via a socket.

I want the application to try to connect to a certain server until the connection is established (i. e. until the server has gone online).

In order to do this, I wrote following code:

procedure SendingThread.Execute();
var
I : integer;
Test : string;
IsConnected : Boolean;
begin
    TcpClient := TIdTCPClient.Create;
    TcpClient.Host := '127.0.0.1';
    TcpClient.Port := 9999;
    TcpClient.ConnectTimeout := 60000;

    IsConnected := false;
    while not IsConnected do
        begin
        try
            TcpClient.Connect;
            IsConnected := True;
        except
            on E:EIdSocketError do
                IsConnected := false;
        end;
    end;
...
end;

When I run this code with server being offline, I get EIdSocketError with error code 10061. The exception is thrown after TcpClient.Connect;.

How can I modify the code so that this exception in caught in the except cause?


Solution

  • There are no code modifications necessary.* Your program is already catching the expected exception.

    What you're seeing is the debugger intercepting the exception as soon as it's thrown, before your program gets a chance to see that an exception exists and do anything about it. When the IDE intrerrupts you, it shows a dialog box giving you the option to continue running. Exercise that option, or press the "Run" button after you dismiss the dialog box.

    You can configure the debugger to ignore some or all exceptions, which is especially helpful when using Indy, which tends to use exceptions for normal flow control. How to do that has been covered here before.

    * Remy's answer describes improvements you can make to your code to catch other possible exceptions.