System.Net is the namespace in the Microsoft Windows .NET Framework that contains the core classes intended for building applications that communicate over a network. The types of functionality found in System.Net range from access to a raw socket to the ability to upload to and download from resources on the Internet. In addition to providing a language-independent, object-oriented way to access network resources, System.Net serves as the communication infrastructure for higher-level, distributed application programming models such as XML-based Web services and .NET Remoting.
Layers of System.Net
The System.Net classes can be divided into four layers, as depicted in Figure 6-1 and described in the following list.
Figure 6-1: The layers of System.Net
1. Basic network types and services: Fundamental types that are used for working with Internet Protocol (IP) addresses or non-IP network addresses such as Internetwork Packet Exchange (IPX). This layer also includes classes for performing Domain Name System (DNS) resolution.
2. Socket-level APIs: A set of managed classes for performing network communication over a socket, such as sending and receiving data between two hosts.
3. Application protocols: Classes intended to be used both on the client and on the middle tier. These classes are for protocols layered on top of the socket APIs, such as the Hypertext Transfer Protocol (HTTP) and the File Transfer Protocol (FTP).
4. Protocol-independent request-response model: These classes provide a pattern that is used by request-response protocols for resolving Uniform Resource Identifiers (URIs) without requiring the developer to write protocol-specific code.
In this section, we’ll walk through each layer and introduce the most commonly used classes for each layer. The layers will be covered in-depth in the following chapters.
Figure 6-2 contains the classes that make up the basic types and services included in System.Net. In the subsequent sections, we’ll look briefly at each of the basic types and services that are made accessible 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 dev