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); 


Pocket PC Network Programming
Pocket PC Network Programming
ISBN: 0321133528
EAN: 2147483647
Year: 2005
Pages: 90

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net