DEVBUZZ Homepage Determing Network Connectivity
 
Web www.devbuzz.com
  HOME PAGE
  All Articles
  Advertise
  Consulting

 Development
  Discuss - Forums
  Still in the box?
  .Compact Framework
  Code Snippets
  SQL Server CE
  Database
  MS Resources
 Stores
  Developer Controls
  Pocket PC Hardware
  Pocket PC Software
  Pocket PC Books
  .NET CF Books
  Book Reviews
  SPB SW Discounts
  RESCO SW Discounts
 DEVBUZZ Info
  About Us
  Help
  Join our email list
  Links & Ratings
  Press & Comments
  Pocket PC version
  Software Reviews
  Hardware Reviews
 Authors
  Authors
  Article Guide
  Competitions
 Resources
  Developers
  Register
  Login

  SPB Discounts!
 Columnists
  Rick Winscot
 Past Blast
  Personal Media Ctr
  Gizmobility
  eVB Legacy
  Old news
  Hosted Software
  Wireless
  Newsletters
  Carl Davis
  Upton Au

 Pocket PC Registry
  Join the registry
  View current list
 Current Poll
Are you converting to .NET Compact Framework?
Yes, it has changed my life!
No, I'm sticking with eVB
.NET CF what's that`?

Current results
3431 votes so far
 Recent Forum Threads [goto forums]

Get Computername
read... (67 hits)


Great aid to development productivity
read... (82 hits)


ThreadingTimer sample code
read... (143 hits)


Multithreading with .NET CF
read... (194 hits)


Moving from eMbedded Visual Basic to Visual Basic .NET
read... (166 hits)


.NET Compact Framework 2.0 Service Pack 2
read... (226 hits)


Transfer Data from SQL Server 2000 to SQL Server Compact Edition
read... (298 hits)


This protocol version is not supported
read... (236 hits)


Converting Lowercase to uppercase wont work
read... (203 hits)


Direct access to MS SQL Server 2000
read... (374 hits)


Creating SDF file in Desktop
read... (513 hits)


Winsock in CF.NET
read... (316 hits)


Using Pocket Outlook to submit HTML page form with MAILTO action
read... (420 hits)


Missing file "System.Data.PocketPC.asmmeta.dll"
read... (268 hits)


HP iPAQ hw6915 Serial Port Issue
read... (309 hits)


Info on the recent forum changes
read... (341 hits)


SqlServer tools from Redgate
read... (383 hits)


Arrow keys and Hardware navigation button
read... (393 hits)


O2 XDA lls pin sync cable to comport
read... (322 hits)


Creating dynamic folders on Pocket PC OS
read... (299 hits)

Custom Windows Mobile software development.
LBS Challenge 2007
LBS Challenge Eight previous NAVTEQ Global LBS Challenge® participants have received venture capital funding and nine past LBS Challenge winners have launched commercial applications on major wireless carriers. Register your non-commercial LBS application in the 2007 NAVTEQ Global LBS Challenge in one of three regions: Americas, Europe-Middle East-Africa (EMEA) or Asia-Pacific(APAC). You could win a share of $2 million in prizes. This could be your year.
Dream. Develop. Win.

Development | .NET Compact Framework

Determing Network Connectivity
Written by W.G. Ryan  [author's bio]  [read 27421 times]
Edited by Derek

.NET Compact Framework   

Page 1 

Determing Network Connectivity

Barely a week goes by without a post or 10 on the various Compact Framework Newsgroups where someone doesn't inquire about network connectivity. Some people are trying to use the full SQL Client namespace which isn't very useful if you aren't connected to a network. Other's ask about invoking a web service in an effort to pull back some data, something else that's kind of sucky if you don't have a network connection. Then there are folks who are trying to move files back and forth from their device to the desktop, and want to have the ability to immediately detect when the device is cradled and show some sort of notification. All of these are fairly common tasks when dealing with PDA development and all of them share one thing in common, namely, the need to determine whether or not the device is cradled or not (I know, techincally you can grab data from a Web Service via GRPS, but assuming one doesn't, it's still a valid thing to inquire about.)

The following code snippet in Devbuzz's Code Snippets section shows how one might go about accomplishing this task:

 
Public Shared Function GotInternet() As Boolean
   Dim req As HttpWebRequest
   Dim res As HttpWebResponse
   Try req = CType(WebRequest.Create("http://www.devbuzz.com"), HttpWebRequest)
   res = CType(req.GetResponse(), HttpWebResponse) req.Abort()
   If res.StatusCode = HttpStatusCode.OK Then
      GotInternet = True
   End If
  Catch weberrt As WebException
     GotInternet = False
  Catch except As Exception
    GotInternet = False
   End Try
End Function

This solution is easy to use, easy to understand, and in most instance gets the job done. All you'd need to do is intermittently call this function via a timer or low priority background thread and you could keep track of whether or not you're connected thereby giving you app the ability to respond accordingly. (Whether or not you should opt for a multi-threaded solution over a timer based one is a whole different can of worms but the logic is the same with either approach).

But there some shortcomings to this approach. For one thing, we've got to remember that PDA's at their present state aren't exactly oozing resources like RAM and processor speed. So, if you polled frequently enough, you'd invariably raise quite a few exceptions when the device wasn't cradled and exceptions aren't the cheapest thing in the world. But a more serious issue might arise if you were using a Smart Phone or device that might need to call into the network at each request. As you can see, that could get ugly quite quickly. But what if you lost your internet connection ? If you have my cable modem service company (who's name coincidentally rhymes with BombBlast) that's a pretty common scenario. You'd end up with False Positives. If someone pulled out the desktop machine's network cable, it'd report a false positive as well (I know, if the desktop wasn't connected to the network then you don't actually have "Network Connectivity" but what we're ultimately trying to determine here is whether or not we are cradled. If our goal was solely to ascertain whether or not we had actual Internet Connectivity, then the approach I'm about to recommend won't work). Finally the web site in question might be down for maintenance or whatever else. In each instance, your device would think it wasn't cradled even though it was.

So how do you resolve this? In order to understand what we're about to do, you'll need some background info on what goes on behind the scenes when you 'cradle' the device. Upon cradling the device, the cradle and desktop machine morph into a new role, that of a network gateway. Now the device's IP Address is going to be something other than Home aka 127.0.0.1. This provides you the ability to infer (accurately) if the device is cradled by virtue of its IP Address. So any instance where IP Address != 127.0.0.1 accurately indicates your device is cradled and any instace where it is, means that it's not.

Now, let me emphasize something here. Just because your IP Address isn't 127.0.0.1 DOESN'T NECESSARILY mean you have internet connectivity. You may or may not, but you can't tell from this alone. However, you can tell from this alone if your device is cradled or not. Secondly, the code snippet above will port over directly to the full framework and you can use it from any desktop app to determine if you can hit the internet or not (provided of course that the web site you are sending requests to is up and running). However, if you use the code below, the logic will break because ActiveSync and the cradle aren't sitting in between your computer and the network but the code WILL compile. The assumption behind the logic no longer holds though and you don't have the benefit of a compiler barking at you to indicate that your logic isn't valid.

With that said, all we need to do is use the System.Net namespace and use DNS to determine the device's IP Address.

VB.NET:

Imports System.Net
#Region "Enums"
Public Enum CradleResult As Integer
     Cradled = 0
     Uncradled = 1
     WebException = 2
     OtherException = 3
End Enum
#End Region


'Class Definition here
Const Home as String = "127.0.0.1"
#Region "Functions"
Public Shared Function IsDeviceCradled() as CradleResult
Try
   Dim HostName as String = Dns.GetHostName()
   Dim IPHost as System.Net.IPHostEntry = Dns.GetHostByName(HostName)
   Dim DeviceIP as String = CType(IPHost.AddressList(0), String)
     If DeviceIP <> IPAddress.Parse(Home).ToString Then
       Return CradleResult.Cradled
     Else
       Return CradleResult.Uncradled
     End If

     Catch webEx as System.Net.WebException
       Return CradleResult.WebException
     Catch othEx as System.Exception
     Return CradleResult.OtherException
End Try

End Function
#End Region

C#

using System.Net;

public enum CradleResult : uint{cradled, uncradled, webException, otherException};

const string Home = "127.0.0.1";

#region "Functions"
public static CradleResult IsDeviceCradled(){
try{
string HostName = Dns.GetHostName();
System.Net.IPHostEntry IPHost = Dns.GetHostByName(HostName);
string DeviceIP = (string)IPHost.AddressList[0];
if(DeviceIP != IPAddress.Parse(Home).ToString()){
return CradleResult.Cradled;
}
else { return CradleResult.Uncradled;}
}
catch(System.Net.WebException webEx){
return CradleResult.WebException;
}
catch(System.Exception othEx){
return CradleResult.othException;
}
}
#endregion

So all we're really doing is using System.Net to determine out IP Address and respond accordingly. The same issue regarding using a timer or a background thread is present here, but that's a whole different matter.

Back to .NET Compact Framework | [Article Index]

 

Back to the top of the page.
Chris De Herrera's Windows CE Website Windows CE News & Information Source
Copyright ©2000-2007 by DEVBUZZ.COM, Inc., NJ. USA.MSDEVELOP