Re: How detect internet connection?
- Posted by jimcbrown (admin) Aug 13, 2013
- 1663 views
Sure:
dns google.com. (your isp or computer may cache this tho)
http to google. (or anywhere else)
When I try to do a "get dns google.com", the program stops responding for several seconds (if no internet). By using the http, this delay also happens.
This delay, I would like to avoid.
For this, I need a solely function to detect whether the internet is available
So, I would make a program ....
if hasInternet () getDNS (MyISP) else - Do nothing end if
... and avoid the delay that I do not wish
It looks like, rather than simply wanting to doing something else while you are waiting (or having an asychronous notification), you want some kind of instantenous notification about the internet being down.
With TCP, this is not possible as the protocol mandates a minimum waiting time.
You could try something like "ping -c 1 -w 1 google.com", which uses ICMP and would return within 1 second - however it's possible for that to return a failure status when the internet connection (and connection to Google) is fully online but simply slow.
You could also parse the output of "ifconfig" and "route -n" to see if any network interfaces are configured and if the gateway is set, etc. If that's not the case, then the connection to the internet is definitely down - and you'd know it almost instantenously.
However, it's possible for those settings to be up and configured correctly, but for the internet connection itself to still be down. There's no surefire way to do this without waiting for a period of time.