Re: Check for internet connection

Greg Robinson <[email protected]>
Newsgroups gmane.comp.windows.devel.dotnet.winforms
Message-ID <[email protected]>
We tackled this 4 years ago with our No Touch Deployed Windows Forms
application.

We wanted to make sure the user could 'see' the web server whether they were
in the office or off site.

We wrote code to ping the web server, check this, check that, etc. We gave
them a friendly dialog say "Hey dummy, you cannot connect to the web
server".

Honestly, the checks got in the way more than they helped. I have gradually
pulled out all of this code and have not seen one issue since.

If the user is 'not on the internet' I honestly do not think it's your
applications responsibly to tell them. Handle the exception and move on
IMHO. It's not worth the effort. Do we check to see if the user can 'see'
the sql server box before we do a db trip?  Most likely not but I bet we do
handle any exceptions when trying to connect..same philosophy IMHO.




Greg Robinson
Custom Data Systems, Inc.
www.cds-am.net



-----Original Message-----
From: Discussion forum for developers using Windows Forms to build apps
and controls [mailto:[email protected]]On Behalf Of
Peter Ritchie
Sent: Sunday, March 18, 2007 8:19 PM
To: [email protected]
Subject: Re: [DOTNET-WINFORMS] Check for internet connection


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)...
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.