问题
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?
回答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.
来源:https://stackoverflow.com/questions/42155019/delphi-6-how-to-set-idtelnet-connect-timeout-for-early-return-on-no-connection