Online/Offline Network Detection with the .NET Framework
Imagine this: you have been working on your great New World Wide Web program. Your very first customer tries it out on their new notebook computer -- perhaps one of those very cute tiny notebooks that people are buying everywhere. And then you get your first report back: “I tried it while walking down the street. Every time I walked by a coffee shop, it failed horribly”. This blog entry will tell you how to use NetworkChange.NetworkAddressChanged to solve your problem.
You have just been hit by the biggest problem in the world of occasionally-connected computing! Your application was fooled into thinking it was talking to your server – that it was online – that it could do something useful. In reality, all it was talking to was the coffee shop, and all the coffee shop was saying was, “please enter the network access code on your receipt to enable networking at this location.” What you need now is online detection, and what you most need to know about online detection is:
You are online if you can connect to your server and you have validated the response.
You might be fooled into looking at the various .Net and native APIs that return a simple boolean that says if you are online or not. Ignore those APIs! They do not provide the answer you need. Why not? Well, there is an entire list of reasons:
1. They mostly say if you are on “a” network, not on “a useful” network or even the internet. Merely being connected to a hub with power is enough for many of them to consider you to be connected.
2. From my personal, hard-fought experience, network detection is an area where some hardware and drivers just do not work reliably. I have even seen a case where configuring a network only worked with a Wizard – manual configuration resulting in a working network but broken online detection code.
3. Many places—for example, the coffee shops and airports – will advertise network connectivity to your computer. But until you enter a special code in your browser (or perform some other action, possibly involving money), the only data that will ever be returned is a request to enter that special code.
4. Some people are on automatic dial-up connection where they pay by the minute. The more modern version of this is the fancy cell-network connections where some data plans will charge by the kilobyte. People with these sorts of connections will be “on line” but will not appreciate your application using the network.
Instead, you have to write your own method that will try to connect to your server, and then check the answer. But wait! When is a good time to check? Well, when your application starts for sure. Other than that – hook up your code to NetworkChange.NetworkAddressChanged. Just make sure that if you are connecting to “your server”, but it is returning nonsense (like, “Welcome to the Airport Coffee Shop...”) then you have to throw in a timer and periodically retry.
One last hint about NetworkAddressChanged– it returns more events than you might think. While testing code for these entries I saw that plugging and unplugging the network cable on my laptop resulted in multiple events being triggered.
MSDN documentation for the NetworkChange.NetworkAddressChanged event is at https://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkchange.networkaddresschanged.aspx
Next up: code patterns to make detection easier.
Comments
Anonymous
March 16, 2009
PingBack from http://blog.a-foton.ru/index.php/2009/03/17/onlineoffline-network-detection-with-the-net-framework/Anonymous
March 17, 2009
Thank you for submitting this cool story - Trackback from DotNetShoutoutAnonymous
October 28, 2009
Hi, I am using the NicConnection class and i noticed something. In its constructor, both the events NetworkChange.NetworkAddressChanged and NetworkChange.NetworkAvailabilityChanged are being wired up. So when i unplug my ethernet cable,both the events are getting fired which is not i want as i am doing something when my network is online/offline and i dont care about the address change.Anonymous
October 30, 2009
Keep in mind that network availability changed doesn't necessarily mean you have a valid address, so depending on your app, you may actually want to key off the address changed event instead. Also keep in mind that network available and a valid address still doesn't mean you can reach the service you're trying to talk to. That said, if you want to ignore one of these when you're in a particular application state, you can certainly do that. In your NetworkAddressChanged callback, for example, you could call NetworkInterface.IsNetworkAvailbable() and take different actions depending on its state.