15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Utility Classes ❘ 655<br />

In this example, the byte integer address is assigned a binary representation of the IP address, <strong>and</strong> the<br />

string ipString is assigned the text “234.56.78.9“.<br />

IPAddress also provides a number of constant static fields to return special addresses. For example, the<br />

Loopback address allows a machine to send messages to itself, whereas the Broadcast address allows<br />

multicasting to the local network:<br />

// The following line will set loopback to "127.0.0.1".<br />

// the loopback address indicates the local host.<br />

string loopback = IPAddress.Loopback.ToString();<br />

// The following line will set broadcast address to "255.255.255.255".<br />

// the broadcast address is used to send a message to all machines on<br />

// the local network.<br />

string broadcast = IPAddress.Broadcast.ToString();<br />

IPHostEntry<br />

The IPHostEntry class encapsulates information relating to a particular host computer. This class<br />

makes the host name available via the HostName property (which returns a string), <strong>and</strong> the AddressList<br />

property returns an array of IPAddress objects. You are going to use the IPHostEntry class in the next<br />

example: DNSLookupResolver.<br />

Dns<br />

The Dns class is able to communicate with your default DNS server to retrieve IP addresses. The two<br />

important (static) methods are Resolve(), which uses the DNS server to obtain the details of a host with a<br />

given host name, <strong>and</strong> GetHostByAddress(), which also returns details of the host but this time using the<br />

IP address. Both methods return an IPHostEntry object:<br />

IPHostEntry wroxHost = Dns.Resolve("www.wrox.com");<br />

IPHostEntry wroxHostCopy = Dns.GetHostByAddress("208.215.179.178");<br />

In this code, both IPHostEntry objects will contain details of the Wrox.com servers.<br />

The Dns class differs from the IPAddress <strong>and</strong> IPHostEntry classes because it has the ability to actually<br />

communicate with servers to obtain information. In contrast, IPAddress <strong>and</strong> IPHostEntry are more along<br />

the lines of simple data structures with convenient properties to allow access to the underlying data.<br />

The Dnslookup example<br />

The DNS <strong>and</strong> IP-related classes are illustrated with an example that<br />

looks up DNS names: DnsLookup (see Figure 24-10).<br />

This sample application simply invites the user to type in a DNS name<br />

using the main text box. When the user clicks the Resolve button, the<br />

sample uses the Dns.Resolve() method to retrieve an IPHostEntry<br />

reference <strong>and</strong> display the host name <strong>and</strong> IP addresses. Note how the host<br />

name displayed may be different from the name typed in. This can occur<br />

if one DNS name (www.microsoft.com) simply acts as a proxy for<br />

another DNS name (lb1.www.ms.akadns.net).<br />

The DnsLookup application is a st<strong>and</strong>ard <strong>C#</strong> Windows application. The<br />

controls are added as shown in Figure 24-10, giving them the names<br />

txtBoxInput, btnResolve, txtBoxHostName, <strong>and</strong> listBoxIPs,<br />

respectively. Then, you simply add the following method to the Form1<br />

class as the event h<strong>and</strong>ler for the buttonResolve Click event:<br />

void btnResolve_Click (object sender, EventArgs e)<br />

{<br />

try<br />

{<br />

IPHostEntry iphost = Dns.GetHostEntry(txtBoxInput.Text);<br />

figure 24-10<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!