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.

656 ❘ ChaPTer 24 netwOrkinG<br />

}<br />

foreach (IPAddress ip in iphost.AddressList)<br />

{<br />

string ipaddress = ip.AddressFamily.ToString();<br />

listBoxIPs.Items.Add(ipaddress);<br />

listBoxIPs.Items.Add(" " + ip.ToString());<br />

}<br />

txtBoxHostName.Text = iphost.HostName;<br />

}<br />

catch(Exception ex)<br />

{<br />

MessageBox.Show("Unable to process the request because " +<br />

"the following problem occurred:\n" +<br />

ex.Message, "Exception occurred");<br />

}<br />

Notice that in this code you are careful to trap any exceptions. An exception might occur if the user types<br />

an invalid DNS name or if the network is down.<br />

After retrieving the IPHostEntry instance, you use the AddressList property to obtain an array containing<br />

the IP addresses, which you then iterate through with a foreach loop. For each entry, you display the IP<br />

address as an integer <strong>and</strong> as a string, using the IPAddress.AddressFamily.ToString() method.<br />

loWer-leVel ProToCols<br />

This section briefly discusses some of the .<strong>NET</strong> classes used to communicate at a lower level.<br />

The System.Net.Sockets namespace contains the relevant classes. These classes, for example, allow you<br />

to directly send out TCP network requests or to listen to TCP network requests on a particular port. The<br />

following table explains the main classes:<br />

Class<br />

Socket<br />

NetworkStream<br />

SmtpClient<br />

TcpClient<br />

TcpListener<br />

UdpClient<br />

PurPose<br />

Deals with managing connections. Classes such as WebRequest, TcpClient, <strong>and</strong><br />

UdpClient use this class internally.<br />

Derived from Stream. Represents a stream of data from the network.<br />

Enables you to send messages (mail) through the Simple Mail Transfer Protocol.<br />

Enables you to create <strong>and</strong> use TCP connections.<br />

Enables you to listen for incoming TCP connection requests.<br />

Enables you to create connections for UDP clients. (UDP is an alternative protocol to TCP<br />

but is much less widely used, mostly on local networks.)<br />

Network communications work on several different levels. The classes you have seen in this chapter so<br />

far work at the highest level — the level at which specific comm<strong>and</strong>s are processed. It is probably easiest<br />

to underst<strong>and</strong> this concept if you think of file transfer using FTP. Although today’s GUI applications hide<br />

many of the FTP details, it was not so long ago when you executed FTP from a comm<strong>and</strong>-line prompt. In<br />

this environment, you explicitly typed comm<strong>and</strong>s to send to the server for downloading, uploading, <strong>and</strong><br />

listing files.<br />

FTP is not the only high-level protocol relying on textual comm<strong>and</strong>s. HTTP, SMTP, POP, <strong>and</strong> other<br />

protocols are based on a similar type of behavior. Again, many of the modern graphical tools hide the<br />

transmission of comm<strong>and</strong>s from the user, so you are generally not aware of them. For example, when you<br />

type a URL into a web browser, <strong>and</strong> the web request goes off to a server, the browser is actually sending a<br />

(plain-text) GET comm<strong>and</strong> to the server, which fulfills a similar purpose as the FTP get comm<strong>and</strong>. It can<br />

also send a POST comm<strong>and</strong>, which indicates that the browser has attached other data to the request.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!