Although not quite as robust and full-featured as the full System.Net namespace found on the desktop, the .NET Micro Framework's slimmed-down version contains the essential set of classes needed to write network applications appropriate for small, resource-constrained embedded devices. That is, this namespace omits higher-level functionality such as HTTP, FTP, and SMTP support, in addition to authentication and secure communications (Secure Socket Layer, or SSL).
Within the System.Net namespace resides a static class, Dns, that represents a connection to one (or more) of the DNS servers that have been configured for your network (see the section titled "Domain Name System" earlier in this chapter for more information). Unlike the full-blown .NET Framework, the .NET Micro Framework version of this class has but a single method, GetHostEntry, which is used to look up a host name (string) and return its associated IP addresses. Because this method can result in more than a single IP address, the IPHostEntry object (also in the System.Net namespace) is used as a wrapper. Listing 6-2 illustrates use of this method in the emulator.
Listing 6-2: Retrieving IP addresses using the DNS
IPHostEntry hostEntry = Dns.GetHostEntry("www.microsoft.com"); int i = 0; foreach (IPAddress ipAddress in hostEntry.AddressList) Debug.Print(i++ + ": " + ipAddress.ToString()); The output from this at the time it was run (your results may vary because of DNS round-robining, a technique used to load balance traffic): 0: 18.104.22.168 1: 22.214.171.124 2: 126.96.36.199 3: 188.8.131.52 4: 184.108.40.206 5: 220.127.116.11
The IPAddress class, found within System.Net, maintains a versatile representation of an IP address, not just its dotted decimal string form, but also as a byte array through the GetAddressBytes instance method. Additionally, there are two static read-only fields: Any and Loopback. The IPAddress.Any field translates to 0.0.0.0 and is used when creating a new socket endpoint to cause the system to listen for activity on any and all active network adapters. The IPAddress.Loopback field translates to 127.0.0.1 and, as described earlier, is a reserved address that represents the local machine.
The EndPoint abstract class represents a generic network resource or service. As we will see, the Connect method of the Socket class accepts an abstract EndPoint as a parameter. Derived classes, such as the IPEndPoint class, represent a specific type of endpoint-in this case, an IP host (SocketAddress) and port. The .NET Micro Framework currently supports only the IPEndPoint derived class, although the .NET Framework additionally supports IrDAEndPoint (for infrared communications).
The abstract EndPoint class provides two methods that must be overridden by derived classes (that is, IPEndPoint): Create and Serialize. The Create method accepts a SocketAddress instance, which represents an IP address in serialized form (that is, raw bytes), and returns an instance of an EndPoint (of the same type as the instance the method is invoked by). Conversely, the Serialize method will extract the SocketAddress from the EndPoint instance.