The .NET Compact Framework
- Networking with the Compact Framework
- Winsock and .NET
- Internet Protocols and the .NET Pluggable Protocol Model
- Consuming Web Services
- Pocket PC and P/Invoke
The human subconscious is a fascinating place—malleable, permeable, fallible.
—Harvey, Farscape
The introduction of the .NET Framework made the last year or so an extremely exciting time for software developers. Not only does .NET provide an entirely new platform for creating software, it also introduces an extremely rich (and quite large) set of class libraries for building managed applications, as well as a new type-safe object-oriented programming language known as C#.
The .NET Compact Framework is a version of .NET specifically designed for small form factor devices, such as Pocket PC. The class library provided with the Compact Framework is extremely similar to its desktop counterpart, except that certain functionality has been “slimmed down” (or entirely eliminated) to better support the limited memory, storage space, and performance of a mobile device.
Because covering the entire Compact Framework would be a book in itself, this chapter provides you with information about using some of the .NET classes that are of particular interest to Pocket PC application developers. We first take a look at performing Winsock communications (see Chapter 1) between devices using the Sockets class library that is provided by the Compact Framework. This is followed by an explanation of how to write applications that request data using standard Internet protocols, such as HTTP (see Chapter 2).
This chapter also describes how you can consume Web Services, probably one of the most intriguing concepts for a mobile developer. A Web Service is a standardized way to access distributed program logic by using “off-the-shelf” Internet protocols. For example, suppose you had an application running on a Pocket PC device that kept an itinerary of your travel plans. You could use one Web Service to get information about flight delays, another to get the weather report at your destination, and another to pull gate information, tying all of the information together within your application. What makes Web Services unique is that any communications with the server hosting the Web Service are done through a standardized XML format. By using Web Services, you can easily create robust mobile applications that pull data from a variety of sources on the Internet.
Finally, we’ll take a look at using some of the APIs that are native to the Pocket PC, such as the Connection Manager (see Chapter 7) and SMS Messaging (see Chapter 8), from applications written in C#.
Unlike writing standard C++ applications for a Pocket PC device using Embedded Visual C++ 3.0, you use Visual Studio 2003.NET for developing C# and VB.NET applications. At this time, you cannot use C++ to develop .NET applications for the Compact Framework.
Networking with the 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 |
Authorization |
Class |
Handles authorization messages to a sever |
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 requests |
HttpVersion |
Class |
Handles version numbers supported by HTTP requests |
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 request |
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 |
WebHeaderCollection |
Class |
Handles protocol headers for a network request or response |
WebProxy |
Class |
Handles HTTP proxy settings |
WebRequest |
Class |
Handles a request to a URI |
WebResponse |
Class |
Handles a response to a URI |
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 (for more information about Internet ports, see Chapter 1).
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 |
AddressFamily |
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. Dns Class Methods
Method |
Description |
---|---|
BeginGetHostByName() |
Starts an asynchronous GetHostByName() request |
BeginResolve() |
Starts an asynchronous Resolve() request |
EndGetHostByName() |
Ends an asynchronous GetHostByName() 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. 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);