TIdTCPClient: Reinstall

I am writing an application that uses TIdTCPClient to communicate with another application through a socket.

I want the application to try to connect to a specific server until the connection is established (that is, until the server goes online).

To do this, I wrote the 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 the server turned off, I get an EIdSocketError with error code 10061 . An exception is TcpClient.Connect; after TcpClient.Connect; .

How can I change the code so that this exception gets into except ?

+4
source share
3 answers

No code changes are required. * Your program is already catching the expected exception.

What you see is a debugger that catches the exception as soon as it throws it before your program gets the opportunity to see that the exception exists and do something. When the IDE starts you up, it displays a dialog box that allows you to continue. Perform this option or click the "Run" button after rejecting the dialog box.

You can configure the debugger to ignore some or all of the exceptions, which is especially useful when using Indy, which typically uses exceptions for normal flow control. How to do it so far.

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

+2
source

The code you showed is the right way to deal with the problem of reconnecting, but there is one small change you need to make. Modify the except block to catch an Exception or EIdException instead of an EIdSocketError . EIdSocketError is not the only possible exception that Connect() can raise. For example, it can raise an EIdConnectException or EIdConnectTimeout , none of which are derived from an EIdSocketError .

 procedure SendingThread.Execute; var ... IsConnected : Boolean; begin ... IsConnected := False; while (not IsConnected) and (not Terminated) do begin try TcpClient.Connect; IsConnected := True; except on E: Exception do IsConnected := False; end; end; ... end; 

You can also just delete the on E sentence as a whole, as it does nothing useful. The IsConnected variable already has the value False when an exception occurs, so there is no need to reassign it with the same value.

 procedure SendingThread.Execute; var ... IsConnected : Boolean; begin ... IsConnected := false; while (not IsConnected) and (not Terminated) do begin try TcpClient.Connect; IsConnected := True; except end; end; ... end; 
+2
source

Although a loop is not needed here, since you are already executing a thread, and this while loop will only run once, always, and if it is not connected, your thread will be in an undefined loop.

-1
source

Source: https://habr.com/ru/post/1446229/


All Articles