Dr. Neil Roodyn

Subscribe to Dr. Neil Roodyn: eMailAlertsEmail Alerts
Get Dr. Neil Roodyn: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn


Article

Network Location Awareness

Knowing your networks

If you are writing a 'WinForms' smart client application for the Windows XP platform then the chances are very high that your application will run on mobile PCs. Sales of mobile PCs (laptops, notebooks, and Tablet PCs) are starting to outnumber the sales of desktop PCs. With this in mind your applications will likely have to be aware of the currently available network connections. This article will briefly describe how you can do this and provide you with links to further resources.

The "Sometimes Connected" Way of Working

Do you own a laptop, notebook, or Tablet PC? Do your customers run your applications on laptops, notebooks, or Tablet PCs?

For most of you the answer is probably yes to at least one of those questions. It's also probable that these mobile PCs will connect to more than one network in the course of a week.

Applications running on these mobile devices need to take into account the fact they will not always have a network connection available to them. When there's a network connection it may not be the same one. Many users now have networks at home as well as at work. Your application may want to distinguish between the two and behave differently. Many mobile PC devices have wireless network connectivity as well as network ports and some have Bluetooth capability as well. This in combination with connections to VPNs (Virtual Private Networks) means that a mobile PC may have multiple network connections open at the same time. Your application should know this and connect through the most appropriate available network. You don't want your application repeatedly attempting to connect to the Internet if no current network connection supports Internet connectivity. This is especially true if the machine is running on battery power; you will be draining the battery needlessly.

The advent of wireless networks has led to a behavior that applications need to cater to - that of being sometimes connected. As your user walks around his office with a Tablet PC you will want your application to carry on working for them seamlessly as they enter and leave networks. The important scenarios that applications should cater to are:

  • Identify current connections
  • Notification of changes
  • Smooth transition from
    - Connected to not connected
    - Not connected to connected
    - Addition of network

Querying the Currently Connected Networks

As a first step it's useful to know which networks the application can currently access.

The .NET Framework doesn't provide this information out-of-the-box. The MSDN help on Network Location Awareness (NLA) points to the Win32 Windows Sockets Library.

Version 2 of the Windows Sockets Library that ships with the Win32 Platform SDK has a number of functions that you can use to detect the currently connected networks. They are:

  • WSALookupServiceBegin - initiates iterating through the currently connected networks
  • WSALookupServiceNext - gets the next available network (or lets us know there are no more network connections)
  • WSALookupServiceEnd - finishes iterating through the networks.
You can create a simple loop to iterate through the currently available networks using these functions, the pseudo-code looks like this:


WSALookupServiceBegin
Do
WSALookupServiceNext
While there are more connected networks
WSALookupServiceEnd

To do this in managed C# you need to use Platform Invoke (known as P/Invoke). There are two structures that are used by the methods we need to call. The WSAData structure is used by the WSAStartup method to get information about the implementation of the Windows Sockets library. You will also need to define the WSAQUERYSET structure that's used by both the WSALookupServiceBegin and the WSALookupServiceNext functions. In the example code in Listing 1 you can see these structures are defined as a C# classes. This is done because in C# a class is always passed by reference, making the call to the methods simpler. In the WSAStartup method the second parameter doesn't have to be marked as an [out] parameter or use the ref keyword. The second parameter is simply an instance of a WSAData class.

Once you have these methods defined you can call them as shown in the example code in Listing 2. This code snippet is simply getting the name of each network and placing it in a list.

Receiving Notifications When Things Change

Now you can determine the networks that are currently available. You can adjust your application to behave in an appropriate way. The next thing you probably want is to be notified of any changes to the currently connected networks. This could be done by polling for the currently connected networks. You could create a worker thread that rebuilds the list of currently connected networks but that wouldn't be very efficient.

There is another function in the Windows Sockets Library that can be called to find out if anything has changed since the last call WSAServiceLookupBegin. The WSANSPIoctl function can be used to provide information as to whether there has been a change to the connected networks.

The best solution is to use the WSANAPIoctl function as a blocking method that only returns when a change has occurred. You can use this in a worker thread that then calls a delegate method in your code.

The sample code in Listing 3 demonstrates this by creating an event that gets raised whenever there is a change in the network connections.

Conclusion

Although it seems like hard work, it's possible to get information on the current network connections. The whitepapers referenced in the resource section below provide a further explanation of how to get more information about each network connection.

The obvious question that many readers will be asking now is "What support will there be for this in the .NET Framework 2.0?" There's good news and bad news.

The System.Net.NetworkInfor-mation namespace will help. Mainly through a class called NetworkInterface that can give you the ability to iterate through the network adaptors attached to the machine. Each network adaptor will be represented by an instance of a NetworkInterface class. This NetworkInterface object will provide some information about the connection made through the adaptor.

From what I currently understand; this information is a subset of what is available from the Win32 NLA functions. So, for example, if you want to check if the connection is on a managed network you will need to use the Win32 NLA methods.

Resources

  • Win 32 SDK documentation: Click Here !
  • Mobile PC Whitepapers: http://msdn.microsoft.com/mobilePC
  • Preliminary Documentation on .NET Framework 2.0: http://msdn2.microsoft.com/library/ce0a4fx0.aspx
  • More Stories By Dr. Neil Roodyn

    Dr. Neil is an independent itinerant consultant, trainer, and author. He travels the world working with software companies. Dr. Neil loves Australia, where he spends the summer enjoying the Sydney lifestyle and helping software development teams get more productive. Dr. Neil spends his other summer each year flying between northern Europe and the USA, working with software teams and writing about his experiences. Neil brings his business and technical skills to the companies he works with to ensure he has happy customers. His latest book, eXtreme .NET, introduces eXtreme Programming Techniques to .NET developers. You can find out more from his Web site www.Roodyn.com.

    Comments (0)

    Share your thoughts on this story.

    Add your comment
    You must be signed in to add a comment. Sign-in | Register

    In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.