Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Know Your Networks

The sometimes-connected nature of mobile computers means that you need to build software that works smoothly as networks connect and disconnect. This article shows you how to build network-aware software using the Network Location Awareness APIs.




Full Text Search: The Key to Better Natural Language Queries for NoSQL in Node.js

he importance of enabling your software to identify the current network connections and change behavior has never been greater, as more people use laptops, notebooks, and Tablet PCs. With the growth of connectivity and online technology come applications that require network connectivity for their supported features, ranging from regular scans for software updates to online data synchronization. Applications that call through to Web services and utilize Internet technologies are becoming commonplace. As these applications run on Mobile PCs, it is important that they can cope with joining to and disconnecting from networks.

There are three states with which an application needs to cope:

  1. There is no network connection.
  2. There is a single network connection.
  3. There are multiple network connections.
A well-behaved software application needs to be able to transition smoothly among these three states. These transitions occur in four possible scenarios:

  1. A network connection becomes available when there was not one previously.
  2. A new network connection becomes available when one (or more) was already available.
  3. A network connection becomes unavailable (drops out), leaving no connections.
  4. A network connection becomes unavailable, leaving one (or more) connections.
In an ideal world, end users should not have to interact when network transitions occur, but it's good practice to provide some non-obtrusive feedback to the user to identify the current state.
The importance of enabling your software to identify the current network connections and change behavior has never been greater.
Current Networks
A good first step is to work out what current network connections are available to the application. The NLA (Network Location Awareness) API provides three methods to help identify the current connections:

  • WSALookupServiceBegin—Initiates iterating through the currently connected networks
  • WSALookupServiceNext—Accesses the next available network (or lets the user know that there are no more network connections)
  • WSALookupServiceEnd—Finishes iterating through the networks.
As these methods are part of the Windows Sockets API, you need to call the WSAStartup method to initialize the process for using the Windows Sockets Library. You must call the WSACleanup method when you have finished using the library. The WSALookupServiceBegin and WSALookupServiceNext methods both use a structure called a WSAQUERYSET. It is used in the WSALookupServiceBegin method to constrain or filter the results returned. The details for each network connection are then returned in a WSAQUERYSET structure from the WSALookupServiceNext method.

Doing this in managed code (C# in this example) requires that you use the Platform Invoke (P/Invoke) to import the Win32 API functions. Listing 1 shows the code needed to define the structures required in C#. Once defined, the methods can be called to build a list of currently connected networks. The WSALookupServiceBegin method creates a dataset of the current connections and the WSALookupServiceNext iterates through each network in that dataset. Finally, calling WSALookupServiceEnd indicates that you have finished with that dataset and that you no longer need it maintained.

The WSALookupServiceBegin method creates a dataset of the current connections.
Listing 2 shows a method that returns an ArrayList of the name of each network connection. Of course, you will probably want more information than just the name of the network. You may notice that in this code listing, a large block of memory is allocated to marshal the network information structure. Later, you'll see how to extract more information about each network and how to allocate the correct amount of memory for the returned information. Knowing the Network Changes
Now that you have an idea of how to get a list of the current network connections, it would be great if your application knew when the connections changed. A dirty solution would be to poll and keep requesting the current changes. This is not a good idea. Most of the devices that use this code are Mobile PCs, and power consumption is an issue for Mobile PCs (see my other article in this issue on Power Management), so polling is not a good solution.

Instead, what you need is some form of notification. The NLA API provides a function called WSANSPIoctl that provides what you need. The WSANSPIoctl function can be used to block the current thread until a change in the currently connected networks has transpired. The function requires that you first call WSALookupServiceBegin to build a list of the current connections. You can then call WSANSPIoctl, which blocks the current thread until there is a change in that list. The code in Listing 3 demonstrates this in the WaitForNetworkChanges method. As this method is going to block until there is a change, run this method in a background worker thread.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date