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.

672 ❘ ChaPTer 25 windOws services<br />

ReadQuotes() is a helper method that reads all the quotes from a file that was specified in the constructor.<br />

All the quotes are added to the List quotes. In addition, you are creating an instance of the<br />

R<strong>and</strong>om class that will be used to return r<strong>and</strong>om quotes:<br />

protected void ReadQuotes()<br />

{<br />

quotes = File.ReadAllLines(filename).ToList();<br />

r<strong>and</strong>om = new R<strong>and</strong>om();<br />

}<br />

Another helper method is GetR<strong>and</strong>omQuoteOfTheDay(). This method returns a r<strong>and</strong>om quote from the<br />

quotes collection:<br />

protected string GetR<strong>and</strong>omQuoteOfTheDay()<br />

{<br />

int index = r<strong>and</strong>om.Next(0, quotes.Count);<br />

return quotes[index];<br />

}<br />

In the Start() method, the complete file containing the quotes is read in the List quotes by<br />

using the helper method ReadQuotes(). After this, a new thread is started, which immediately calls the<br />

ListenerThread() method — similarly to the TcpReceive example in Chapter 24, “Networking.”<br />

Here a thread is used because the Start() method cannot block <strong>and</strong> wait for a client; it must return<br />

immediately to the caller (SCM). The SCM would assume that the start failed if the method didn’t return<br />

to the caller in a timely fashion (30 seconds). The listener thread is set as a background thread so that the<br />

application can exit without stopping this thread. The Name property of the thread is set because this helps<br />

with debugging, as the name will show up in the debugger:<br />

public void Start()<br />

{<br />

ReadQuotes();<br />

listenerThread = new Thread(ListenerThread);<br />

listenerThread.IsBackground = true;<br />

listenerThread.Name = "Listener";<br />

listenerThread.Start();<br />

}<br />

The thread function ListenerThread() creates a TcpListener instance. The AcceptSocket() method<br />

waits for a client to connect. As soon as a client connects, AcceptSocket() returns with a socket associated<br />

with the client. Next, GetR<strong>and</strong>omQuoteOfTheDay() is called to send the returned r<strong>and</strong>om quote to the client<br />

using socket.Send():<br />

protected void ListenerThread()<br />

{<br />

try<br />

{<br />

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

listener = new TcpListener(ipAddress, port);<br />

listener.Start();<br />

while (true)<br />

{<br />

Socket clientSocket = listener.AcceptSocket();<br />

string message = GetR<strong>and</strong>omQuoteOfTheDay();<br />

UnicodeEncoding encoder = new UnicodeEncoding();<br />

byte[] buffer = encoder.GetBytes(message);<br />

clientSocket.Send(buffer, buffer.Length, 0);<br />

clientSocket.Close();<br />

}<br />

}<br />

catch (SocketException ex)<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!