0

I need to connect to a device on the local network, using a TCP/IP connection. When I use a TIdTCPClient, all works well, except one thing:

If the connection is not available, it takes about 18-20 seconds before I get a timeout. The property ConnectTimeout has no effect, no matter what values I set. It always takes the same amount of time before a timeout.

This answer mentions long delay times for a timeout, and I am wondering if that's related to the Indy components?

I have to find out if the connection cannot be established very quickly, let's say within 1 second at most.

Is there a way to do this using Indy, or do I need to use different components / a different approach?

(I'm using the Indy 10 version that shipped with Delphi 2009)

EDIT:
I followed the instructions to upgrade Indy to the latest version in this post.

Still the same, it now consistently takes 22 seconds until TCPClient.Connect returns when there is no connection. ConnectTimeout and/or ReadTimeout seem to have no influence on this.

Community
  • 1
  • 1
Holgerwa
  • 3,430
  • 9
  • 42
  • 50
  • If you want a quick non-blocking delphi socket solution, try ICS - http://www.overbyte.be/frame_index.html – Warren P Feb 16 '11 at 02:43
  • @Misha: I get a connection timeout, as expected, but I cannot wait 20 seconds for this to happen. If there is a connection problem, I need to know right away. – Holgerwa Feb 17 '11 at 08:01
  • Are you using an IP address or a host name? Could this be a DNS host name resolution issue? – Misha Feb 17 '11 at 11:28
  • @Misha: I'm using the IP address of a device on the same LAN. No name resolving. – Holgerwa Feb 17 '11 at 12:18

3 Answers3

5

ConnectTimeout works correctly for 2010 and XE. Perhaps you can update your Indy version to the latest (its free). I have a function that does 'quick check' connects, just to check availability of the device and those timeouts are 5 seconds without problems (in both 2010 and XE).

Gregor Brandt
  • 7,659
  • 38
  • 59
  • I have updated Indy to the latest version, but still the same problem (see the EDIT in my question) – Holgerwa Feb 16 '11 at 13:48
2

With a default TCP client connect timeout (not specifically set) and a read timeout of 1 second, using Delphi 2010 and the latest Indy version, a local connection (using localhost as the host name) times out in 1 second. So this is definitely not a Delphi/Indy issue. BTW, this gives me an EIdSocketError ("socket error # 10061 connection refused").

Misha
  • 1,816
  • 1
  • 13
  • 16
1

I had the same exact problem. Check out this StackOverflow post.

In short, because Indy threads are blocking, you will need to make a threaded process. Then in the primary application create a timer which will terminate the thread if it has not done what it is supposed to do in the time given.

After I implemented this is worked great.

Community
  • 1
  • 1
M Schenkel
  • 6,294
  • 12
  • 62
  • 107
  • I have the TCPClient in a separate thread, but while it tries to connect (my 20 second wait time) I cannot terminate the thread. It just waits until the connect times out and then the thread terminates. – Holgerwa Feb 16 '11 at 13:46
  • I think I was doing the same thing until I took note of @Kennedy comment on the post I reference where he says "Asking the thread to terminate itself won't really work". The key is to explicitly tell the TCPClient to disconnect from the main thread after the 20 second wait time: mThread.IdHTTP.Disconnect; – M Schenkel Feb 17 '11 at 16:36
  • @Holgerwa Indy uses a thread already within the Connect code. Moving the Indy code to a separate thread is good design because it keeps the application responsive. But It does not answer the question why connecting takes much longer than the specified interval. – mjn Sep 16 '16 at 16:13