Re: Check for internet connection
Peter Ritchie <[email protected]>
| Newsgroups | gmane.comp.windows.devel.dotnet.winforms |
|---|---|
| Message-ID | <LISTSERV%[email protected]> |
There's no way to tell if a computer is not connected to the Internet 100% reliably. There are certain things you can do to mitigate timeouts and exceptions; but you must make sure you handle those exceptions in case the connection goes down after your check and before you attempt your first connection. The simplest, is to just catch the exception; as you've seen. There's two parts to a connection, the name resolution and the connection. The name resolution is where the exception is being thrown. There's also a bug in the framework with respect to asynchronous IO via an HTTP connection; the name lookup part isn't performed asynchronously... You can use the Dns.GetHostEntry() to manually perform the name lookup; but if it can't connect to the NIC's DNS server an exception will be thrown. Without PInvoking WinSock's gethostbyname you'll have to catch that exception. You can mitigate that by using NetworkInterface.GetIsNetworkAvailable(). Again, this only mitigates connection issues. GetIsNetworkAvailable gets the operational status of the non-tunnel and non-loopback interfaces at that instance in time; it could very well have just been dropped (it will return true if just one of the interfaces is considered operational). Plus, it doesn't do anything to test if your DNS server is up or not-- which will result in the same exception if the DNS server is not responding (i.e. there's no way to tell from an connection exception if your Internet isn't working or the DNS isn't responding). Further, you could use the IPInterfaceProperties.DnsAddress property on each applicable result of NetworkInterface.GetIPProperties() and use NetworkInformation.Ping to ping the DNS before a name resolution or a connection attempt... Apologies if parts of this is a repeat of previous posts, I simply read your last post about not having to handle the exception and tried to put two and two together (but may have resulted in three)...