Delphi 6. How to set idTelnet.Connect timeout for early return on no connection

喜你入骨 提交于 2019-12-25 16:50:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!