elopers 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. ")
Dim userName As String
userName = Console.ReadLine()
Console.WriteLine("Please enter your password. ")
Dim password As String
password = Console.ReadLine()
Dim client As New WebClient
client.Credentials = New NetworkCredential(userName, password)
Notice that in this code the credentials are obtained by prompting the user to enter them on the command line. System.Net also provides a way of using the default credentials of the account under which the application is running. You do so by setting the Credentials property on a class equal to CredentialCache.DefaultCredentials. Whenever possible, developers should use default credentials instead of prompting the user or reading credentials from some other source because default credentials are obtained through the security systems integrated into the underlying operating system.
DNS can be used to represent nodes on the network with human-readable names rather than IP addresses. For example, imagine trying to remember a string of characters such as www.contoso.com instead of a series of numbers such as 220.127.116.11. One feature provided by DNS is the ability to have multiple IP addresses assigned to one host name. You can even have different IP address types (for example, IPv4 and IPv6) assigned to the same host. It’s often useful to be able to resolve a name to the list of IP addresses associated with that name.
Access to DNS is enabled and controlled in System.Net through the Dns, DnsPermission, and IPHostEntry classes. DNS is covered in more detail in Chapter 7. The following example shows the basic steps involved in resolving a name and outputting the corresponding IP addresses to the console using System.Net.
static void ResolveName(string name)
IPHostEntry hostInfo = Dns.Resolve(name);
foreach(IPAddress address in hostInfo.AddressList)
Sub ResolveName(ByVal name As String)
Dim hostInfo As IPHostEntry
hostInfo = Dns.Resolve(name)
Dim address As IPAddress
For Each address In hostInfo.AddressList
Catch ex As Exception