0

Calling idTelnet.Connect normally connects to the remote device instantaneously.
But,
if the remote device does not respond, then the call to idTelnet.Connect does not return.
It waits for a response from the remote device.

This hangs the entire application.

How can I set a timeout so that idTelnet.Connect returns within nn ms
regardless of whether a connection has been established or not?

mjn
  • 36,362
  • 28
  • 176
  • 378
AndersJ
  • 411
  • 1
  • 3
  • 15
  • I recommend to run the code in a background thread. – mjn Feb 10 '17 at 10:00
  • Is there a ConnectTimeout property? If not, I would try the current Indy 10.6.2 – mjn Feb 10 '17 at 10:00
  • In Indy 10, `TIdTCPClient` has a `ConnectTimeout` property. In Indy 9, there is no `ConnectTimeout` property, but `Connect()` has an optional `ATimeout` parameter instead. – Remy Lebeau Feb 10 '17 at 23:04

1 Answers1

1

if the remote device does not respond, then the call to idTelnet.Connect does not return.

Yes, it will - eventually.

It waits for a response from the remote device.

Or until the OS finally gives up and fails the connection, reporting an error that Indy will then raise as an exception.

This hangs the entire application.

That means you are calling Connect() in the context of the main UI thread, which you should not be doing in the first place. If you must do that, at least place a TIdAntiFreeze component on your MainForm (and be prepared to handle any reentry consequences that may introduce). Otherwise, move your socket code to a separate worker thread instead.

How can I set a timeout so that idTelnet.Connect returns within nn ms regardless of whether a connection has been established or not?

You did not say which version of Indy you are using. Delphi 6 is very old. If you are using the version of Indy that shipped with it, then you are using Indy 8 or maybe 9. Connect() has no timeout functionality at all in Indy 8. In Indy 9, Connect() has an optional ATimeout parameter. In Indy 10, the ATimeout parameter was replaced with a new ConnectTimeout property.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Thanks Remy forbsummarizing the issue. – AndersJ Feb 11 '17 at 01:49
  • Can Delphi 6 be upgraded to Indy 10? – AndersJ Feb 11 '17 at 01:59
  • [Yes, it can](http://ww2.indyproject.org/Sockets/Docs/Indy10Installation.EN.aspx). In fact Indy 10 supports all the way back to Delphi 5. – Remy Lebeau Feb 11 '17 at 02:58
  • I just started another question/topic asking for help to upgrade to Indy 10. Should I have continued here instead? I decided it would be cleaner to start a new one. I appologize if that was a bad decision. – AndersJ Feb 11 '17 at 03:10
  • @AndersJ: Posting a new question is correct, since you are asking a new question that is not related to this question about `Connect()` timeouts. – Remy Lebeau Feb 11 '17 at 03:17