Book Excerpt

Networking with Compact Framework

When developing applications that communicate over a network using .NET, most of the classes that you will need to familiarize yourself with are part of the System.Net namespace. It contains classes for handling Internet communications with objects that support proxy servers, IP addresses, DNS name resolution, network data streams, and specific classes for handling pluggable protocols such as the Hypertext Transfer Protocol (HTTP).

Table 12.1 describes the objects contained in the System.Net namespace.

Table 12.1 The System.Net Namespace

Name Object Type Description
AuthenticationManager Class Manages authentication models
AuthenticationManager Class Handles authorization messages to a server
DNS Class Handles domain name resolution
EndPoint Class Abstract class for identifying a network address
GlobalProxySelection Class Handles the default proxy for HTTP requests
HttpContinueDelegate Delegate Callback used for HTTP requests
HttpStatusCode Enumeration Status codes used for HTTP
HttpVersion Class Handles version numbers
HttpWebRequest Class Handles an HTTP request
HttpWebResponse Class Handles the response of an HTTP request
IAuthenticationModule Interface Interface used for Web authentication
ICertificatePolicy Interface Interface that validates a server's certificate
ICredentials Interface Interface for handling Web client authentication
IPAddress Class Handles IP addressing
IPEndPoint Class Handles an IP address and port number
IPHostEntry Class Handles Internet host address information
IrDAEndPoint Class Handles an infrared connection to another device
IWebProxy Interface Interface to handle a proxy
IWebProxy Interface Interface to handle a proxy
IWebRequestCreate Interface Interface to handle new WebRequest instances
NetworkCredential Class Handles network usernames and passwords
ProtocolViolationException Class Exception used when a network protocol error occurs
ServicePoint Class Handles connection management for HTTP
ServicePointManager Class Handles a collection of ServicePoint classes
SocketAddress Class Stores information from EndPoint classes
WebException Class Exception used when an error occurs accessing the network
WebExceptionStatus Enumeration Status codes used with the WebException class
WebExceptionStatus Enumeration Status codes used with the WebException class
WebHeaderCollection Class Handles protocol headers for a network request or response
WebProxy Class Handles HTTP proxy settings
WebRequest Class Handlles a request to a URL
WebResponse Class WebResponse

TCP/IP Addresses

In Chapter 1, you learned about the Internet Protocol version 4 (or IPv4) address scheme on Pocket PC. You may remember that an IPv4 address is used by a device to specify its unique host and subnet address, which it uses to communicate over a TCP/IP network. All of the methods and properties that are needed to manage an Internet address within the Compact Framework are handled by the System.Net.IPAddress class.

The IPAddress constructor is defined as follows:
public IPAddress(long newAddress);

The only parameter needed is the 32-bit value of the IP address. The class also contains the methods and properties described in Table 12.2.

Table 12.2 IPAddress Class Methods and Properties

Method Description
HostToNetworkOrder() Converts from host byte order to network byte order
IsLoopback() Returns TRUE if the network address is the loopback adapter
NetworkToHostOrder() Converts from network byte order to host byte order
Parse() Converts a string to an IPAddress class

Property Get/Set/Read-Only Description
Address Get/set Value of the IP address
Any Read-only field Indicates that the IP address is used for all network adapters
Broadcast Read-only field Returns the IP broadcast address
Loopback Read-only field Returns the IP loopback address
None Read-only field Indicates that the IP address is not used for any network adapter

One of the most useful methods in the IPAddress class is the Parse() method. You can use this to easily construct a new IPAddress object using the standard dotted-notation Internet address, as shown in the following example:

System.Net.IPAddress localIPAddress =
System.Net.IPAddress.Parse("127.0.0.1");

Although the IPAddress class by itself is useful for managing an Internet address, most of the networking functions in the Compact Framework use the System.Net.IPEndPoint class to specify another machine on the network. An IPEndPoint not only specifies the IP address of the remote connection, but also contains information about the port that will be used to connect with the service running on the remote device.

There are two ways to construct a new IPEndPoint class. The first method takes the 32-bit value of the IP address and a port:

public IPEndPoint(long address, int port);

You can also create a new IPEndPoint by passing in a previously created IPAddress object:

public IPEndPoint(IPAddress address, int port);

The following code shows how you can create an IPEndPoint that represents a connection to the local machine on port 80:

System.Net.IPAddress localIPAddress

System.Net.IPAddress.Parse("127.0.0.1");
System.Net.IPEndPoint localIPEndpoint = new
System.Net.IPEndPoint(localIPAddress, 80);

The IPEndPoint class consists of the methods and properties described in Table 12.3.

Table 12.3 IPEndPoint Class Methods and Properties

Method Description
Create() Creates an IPEndPoint based on an IP address and port
Serialize() Serializes IPEndPoint information into a SocketAddress instance

Property Get/Set/Read-Only Description
Address Get/set Value of the IP address
Address Family Get Gets the address family for the IP address
Port Get/set Value of the port
MaxPort Read-only field Specifies the maximum value for the port
MinPort Read-only field Specifies the minimum value for the port

Name Resolution

The resolution of a domain name (such as www.furrygoat.com) or IP address is handled by the System.Net.Dns class. It contains the methods described in Table 12.4.

Table 12.4 shows Dns Class Methods

Method Description
BeginGetHostByName() Starts an asynchronous GetHostByName() request
BeginResolve() Starts an asynchronous Resolve() request
EndResolve() Ends an asynchronous Resolve() request
GetHostByAddress() Gets host information based on the IP address
GetHostByName() Gets host information based on the name
Resolve() Resolves a host name or IP address to an IPHostEntry() class

After the DNS resolution process has completed, information about the domain is stored in a new instance of the System.Net.IPHostEntry class. The class has the properties described in Table 12.5.

Table 12.5 shows IPHostEntry Class Properties

Property Get/Set/Read-Only Description
AddressList Get/set Gets or sets a list of IPAddress objects associated with the host
Aliases Get/set Gets or sets a list of aliases associated with the host
HostName Get/set Gets or sets the DNS host name

The following code shows how you can create an IPEndPoint that is associated with the Microsoft Web Server by using the System.Net.Dns class to first resolve the IP address:

// Resolve the MS Web Server IP address
System.Net.IPHostEntry microsoftHost =
   System.Net.Dns.GetHostByName("www.microsoft.com");

// Copy the resolved IP address to a string
String msIP = microsoftHost.AddressList[0].ToString();

// Create the endpoint
System.Net.IPEndPoint microsoftEndPoint = new
   System.Net.IPEndPoint(microsoftHost.AddressList[0], 80); 

Use the following table of contents to navigate to chapter excerpts, or click here to view Chapter 12 in its entirety.


.NET Compact Framework
  Home:Introduction
 Part 1:.NET Compact Framework
 Part 2: Networking with the Compact Framework
 Part 3: Winsock, .NET and the Compact Framework
 Part 4: Internet Protocols and the .NET Pluggable Protocol Mode
 Part 5: Consuming Web Services and the Handheld Device
 Part 6: Pocket PC and P/Invoke
ABOUT THE BOOK:   
Pocket PC Network Programming is a comprehensive tutorial and reference for writing network applications on Pocket PC 2002 and Pocket PC 2002 Phone Edition devices. It explains how the Pocket PC communicates with the Internet, with other mobile devices, and with networks. Click here to purchase the book from Addison-Wesley.
ABOUT THE AUTHOR:   
Steve Makofsky is a software design engineer on Microsoft's .NET XML Messaging team. In addition to having been a Microsoft Embedded MVP, he has worked on several commercial Windows CE products, including the award-winning bUSEFUL Utilities 1.0 and 2.0 (Best of Comdex Utility 1998/1999). Steve coauthored Teach Yourself Windows CE Programming in 24 Hours (Sams, 1999) and has published several magazine articles on .NET and mobile device development. When not working on cool embedded projects, Steve likes to drink lattes and hike on Mt. Everest.

This was first published in November 2006

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: