15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

654 ❘ ChaPTer 24 netwOrkinG<br />

UriBuilder, however, implements fewer properties, just enough to allow you to build up a complete URI.<br />

These properties are read-write.<br />

You can supply the components to build up a URI to the constructor:<br />

UriBuilder MSPage = new<br />

UriBuilder("http", "www.microsoft.com", 80, "SomeFolder/SomeFile.htm");<br />

Or, you can build the components by assigning values to the properties:<br />

UriBuilder MSPage = new UriBuilder();<br />

MSPage.Scheme ="http";<br />

MSPage.Host = "www.microsoft.com";<br />

MSPage.Port = 80;<br />

MSPage.Path = "SomeFolder/SomeFile.htm";<br />

After you have completed initializing the UriBuilder, you can obtain the corresponding Uri object with<br />

the Uri property:<br />

Uri CompletedUri = MSPage.Uri;<br />

iP addresses <strong>and</strong> dns names<br />

On the Internet, you identify servers as well as clients by IP address or host name (also referred to as a<br />

DNS name). Generally speaking, the host name is the human-friendly name that you type in a web browser<br />

window, such as www.wrox.com or www.microsoft.com. An IP address is the identifier computers use to<br />

identify each other. IP addresses are the identifiers used to ensure that web requests <strong>and</strong> responses reach the<br />

appropriate machines. It is even possible for a computer to have more than one IP address.<br />

Today, IP addresses are typically a 32-bit value. An example of a 32-bit IP address is 192.168.1.100. This<br />

format of IP address is referred to as Internet Protocol version 4. Because there are now so many computers<br />

<strong>and</strong> other devices vying for a spot on the Internet, a newer type of address was developed — Internet<br />

Protocol version 6. IPv6 provides a 64-bit IP address. IPv6 can potentially provide a maximum number of<br />

about 3 × 10 28 unique addresses. You will find that the .<strong>NET</strong> Framework allows your applications to work<br />

with both IPv4 <strong>and</strong> IPv6.<br />

For host names to work, you must first send a network request to translate the host name into an IP address,<br />

a task carried out by one or more DNS servers.<br />

A DNS server stores a table mapping host names to IP addresses for all the computers it knows about,<br />

as well as the IP addresses of other DNS servers to look up the host names it does not know about. Your<br />

local computer should always know about at least one DNS server. Network administrators configure this<br />

information when a computer is set up.<br />

Before sending out a request, your computer will first ask the DNS server to tell it the IP address<br />

corresponding to the host name you have typed in. When it is armed with the correct IP address, the<br />

computer can address the request <strong>and</strong> send it over the network. All this work normally happens behind<br />

the scenes while the user is browsing the Web.<br />

.neT Classes for iP addresses<br />

The .<strong>NET</strong> Framework supplies a number of classes that are able to assist with the process of looking up IP<br />

addresses <strong>and</strong> finding information about host computers.<br />

IPAddress<br />

IPAddress represents an IP address. The address itself is available as the GetAddressBytes property <strong>and</strong><br />

may be converted to a dotted decimal format with the ToString() method. IPAddress also implements<br />

a static Parse() method, which effectively performs the reverse conversion of ToString() — converting<br />

from a dotted decimal string to an IPAddress:<br />

IPAddress ipAddress = IPAddress.Parse("234.56.78.9");<br />

byte[] address = ipAddress.GetAddressBytes();<br />

string ipString = ipAddress.ToString();<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!