0

I try to use getaddrinfo() in my application which should determine whether I am connected to the internet. As long as getaddinfo() is able to return successfully everything is OK. But as soon as I disconnect my computer from the router the execution of the function fails and takes about 10 seconds.

Is there a solution to fix this or is there a better function to check whether I am connected to the internet or not?

Note: A similar question has already been asked here but disabling the DNSClient service didn't work for me.

Community
  • 1
  • 1
ThunderStorm
  • 766
  • 8
  • 24
  • 3
    If you consider how `getaddrinfo` works, this is absolutely normal. It looks up the address in `hosts` and if that does not work, sends a request to the primary DNS server. That server will do a lookup in its database, and if it can't find anything forward the request to the next DNS server in the hierarchy. Which will do the same until up to the root. This can take some time. You're using the wrong funciton, though. The `InternetGetConnectedState` function exists for a reason. – Damon May 12 '14 at 16:53
  • Ok thank you. Is there an other function for my purpose which respondes faster? – ThunderStorm May 12 '14 at 16:56
  • @ThunderStorm How could there be? Do you want to know the answer or not? – David Heffernan May 12 '14 at 16:58
  • I edited out the screenshot because it wasn't actually showing the duration of the `getaddrinfo` call. Your statement that it is taking 10 seconds is enough. – nobody May 12 '14 at 16:59
  • See also: http://stackoverflow.com/questions/2677702/detect-internet-v-local-lan-connection – nobody May 12 '14 at 17:04
  • @DavidHeffernan I'm sorry I think I didn't see the edit of Damons' comment. – ThunderStorm May 12 '14 at 17:05
  • In general, if a blocking function takes longer than you like to time out, you can move the call to a background thread and signal an event when it completes. The original calling thread can wait (with whatever timeout you like) on the event. – nobody May 12 '14 at 17:07
  • @Damon And sending the request to its primary DNS server will fail, probably with a timeout waiting for the response. After which, it will try the secondary DNS server, and so on. It still shouldn't take 10 seconds (unless he's got 20 or so DNS servers). – James Kanze May 12 '14 at 17:54
  • @AndrewMedico Thank you but in my case it was necessary to get the response quite fast. – ThunderStorm May 12 '14 at 17:55
  • If you're referring to my thread technique comment, fast is not an issue. The usual wait function on Windows, `WaitForSingleObject` takes a timeout specified in milliseconds. You can make it as short as you like. – nobody May 12 '14 at 18:10
  • @JamesKanze: It might. RFC 1035 suggests a minimum retransmission interval of 2-5 seconds, and each subsequent retry be 50-100% longer than the previous. Typical implementations retry 4 times, so that's up to 60 seconds in the worst case (assuming the implementation doesn't retry using TCP, in the _very_ worst case, too!). – Damon May 12 '14 at 18:26
  • @Damon Now I tried using `InternetGetConnectedState()` but the flag is always 0x12 even though I am not connected to the internet. I think I did something wrong. – ThunderStorm May 12 '14 at 19:25
  • @Andrew if you don't wait for a decent amount of time, you may get the wrong answer – David Heffernan May 12 '14 at 22:19
  • @Thunder What's wrong with 0x12? That would appear to be normal. What does the function return? TRUE or FALSE. You need to read the docs again. – David Heffernan May 12 '14 at 22:21
  • @DavidHeffernan I'm aware of that. It's the implementer's judgement call to find the balance between "too slow" and "false not-connected conclusions". – nobody May 12 '14 at 22:21
  • @DavidHeffernan I was confused, because `InternetGetConnectedState(&flags,0);` keeps returning **true** even though I unplugged my LAN cable. – ThunderStorm May 14 '14 at 19:25

1 Answers1

0

The BOOL InternetGetConnectedState( Out LPDWORD lpdwFlags, In DWORD dwReserved ) function could be a good choice. Thanks to Damon.


The InternetGetConnectedState function exists for a reason. - @Damon

ThunderStorm
  • 766
  • 8
  • 24