ccessible through System.Net. Although this section introduces the key types, most of them are covered in- depth in the chapters that follow.
Figure 6-2: Basic types and services provided by System.Net
The IPAddress type provides a class representation of an IP address. It includes methods that can be used to perform common operations on an IP address, such as parsing the address. The following code shows how you would parse an IP address.
static void ParseIPAddress(string addressString)
IPAddress address = IPAddress.Parse(addressString);
Console.WriteLine("The address is " + address.ToString() + " and is of the family "
+ address.AddressFamily.ToString() + ".");
Console.WriteLine("Failure parsing " + addressString + ". " + ex.ToString());
Sub ParseIPAddress(ByVal addressString As String)
Dim address As IPAddress
address = IPAddress.Parse(addressString)
Console.WriteLine("The address is " & address.ToString _
& " and is of the family " & _
address.AddressFamily.ToString + ".")
Catch ex As Exception
Console.WriteLine("Failure parsing" & addressString & _
". " & ex.ToString)
Note that if an invalid IP address is specified in this case, IPAddress will throw a FormatException that should be handled by the application. The IPAddress class should be used for validation any time your application receives an IP address as input from a user, a configuration file, or another source. IPAddress also provides the ability to determine whether a particular address is a member of the Internet Protocol version 6 (IPv6) or the Internet Protocol version 4 (IPv4) address family. Finally, IPAddress contains a number of helper methods and fields, such as IPAddress.Any, IPAddress.IPv6Any, and IPAddress.IsLoopback. The Any fields can be used to indicate that the application should bind to a port that listens on all network interfaces of that address family. The IsLoopback method can be useful when validating whether a local loopback address is being used.
Working with Different Address Families
The System.Net.EndPoint class provides developers with a way to access resources through System.Net over protocols that belong to different address families that have different semantics at the addressing level. This functionality is useful when you need to write an application that talks over a non-IP protocol, such as AppleTalk, IPX, or infrared. Because IP-based protocols are the most commonly used on today’s networks, System.Net includes an IPEndPoint type in the .NET Framework. In the case of IPEndPoint, an end point is defined as an IP address and a port number. The .NET Compact Framework includes an IrDAEndPoint implementation of the EndPoint class that can be used for communicating with other nodes using infrared technologies. In the case of an IrDAEndPoint, an end point is defined as a device ID and a service name. Although most developers will simply work within the confines of the IP address family, this extensibility point is useful because the definition of an end point often varies from one address family to another.
System.Net defines an ICredential interface and an associated NetworkCredential class that provide a means of specifying network credentials, such as a user name and a password in cases where network authentication is required when accessing a resource. These classes are mainly used in the context of the HTTP application protocol classes. However, in newer release of the .NET Framework, their use is likely to be expanded to include other socket-level authentication scenarios as well. The following code demonstrates how you can use the NetworkCredential class to authenticate with a remote resource.
static void RequestProtectedResource(string resource)
Console.WriteLine("Please enter your user name. ");
string userName = Console.ReadLine();
Console.WriteLine("Please enter your password.");
string password = Console.ReadLine();
WebClient client = new WebClient();
client.Credentials = new NetworkCredential(userName, password);
Sub RequestProtectedResource(ByVal resource As String)
Console.WriteLine("Please enter your user name. ")
