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.

660 ❘ ChaPTer 24 netwOrkinG<br />

using System.Net;<br />

using System.Net.Sockets;<br />

using System.Threading;<br />

using System.Windows.Forms;<br />

Inside the form’s constructor, you spin up a background thread:<br />

public Form1()<br />

{<br />

InitializeComponent();<br />

Thread thread = new Thread(new ThreadStart(Listen));<br />

thread.Start();<br />

}<br />

The remaining important code is this:<br />

public void Listen()<br />

{<br />

IPAddress localAddr = IPAddress.Parse("127.0.0.1");<br />

Int32 port = 2112;<br />

TcpListener tcpListener = new TcpListener(localAddr, port);<br />

tcpListener.Start();<br />

TcpClient tcpClient = tcpListener.AcceptTcpClient();<br />

}<br />

NetworkStream ns = tcpClient.GetStream();<br />

StreamReader sr = new StreamReader(ns);<br />

string result = sr.ReadToEnd();<br />

Invoke(new UpdateDisplayDelegate(UpdateDisplay),new object[] {result} );<br />

tcpClient.Close();<br />

tcpListener.Stop();<br />

public void UpdateDisplay(string text)<br />

{<br />

txtDisplay.Text= text;<br />

}<br />

protected delegate void UpdateDisplayDelegate(string text);<br />

The thread begins execution in the Listen() method <strong>and</strong> allows you to make the blocking call to<br />

AcceptTcpClient() without halting the interface. Notice that the IP address (127.0.0.1) <strong>and</strong> the port<br />

number (2112) are hard-coded into the application, so you will need to enter the same port number from the<br />

client application.<br />

You use the TcpClient object returned by AcceptTcpClient() to open a new stream for reading. As with<br />

the earlier example, you create a StreamReader to convert the incoming network data into a string. Before<br />

you close the client <strong>and</strong> stop the listener, you update the form’s text box. You do not want to access the text<br />

box directly from your background thread, so you use the form’s Invoke() method with a delegate <strong>and</strong> pass<br />

the result string as the first element in an array of object parameters. Invoke() ensures that your call is<br />

correctly marshaled into the thread that owns the control h<strong>and</strong>les in the user interface.<br />

TCP Versus udP<br />

The other protocol covered in this section is UDP (User Datagram Protocol). UDP is a simple protocol with<br />

few features <strong>and</strong> little overhead. Developers often use UDP in applications where the speed <strong>and</strong> performance<br />

requirements outweigh the reliability needs, for example, video streaming. In contrast, TCP offers a<br />

number of features to confirm the delivery of data. TCP provides error correction <strong>and</strong> retransmission in<br />

the case of lost or corrupted packets. Last, but hardly least, TCP buffers incoming <strong>and</strong> outgoing data <strong>and</strong><br />

also guarantees that a sequence of packets scrambled in transmission is reassembled before delivery to the<br />

application. Even with the extra overhead, TCP is the most widely used protocol across the Internet because<br />

of its high reliability.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!