The processing of whatever request its handling at

Info icon This preview shows pages 558–560. Sign up to view the full content.

View Full Document Right Arrow Icon
the processing of whatever request it’s handling at the moment before starting work on the next one. It’s common for a server to have hundreds of inbound connections open simultaneously.) But since this particular service can do all the work it needs to do and then close the connection immediately, it doesn’t have any particular reason to open several connections simultaneously. The code that does the work here is pretty similar to the client code we saw in Exam- ple 13-24 . As before, we create a NetworkStream , passing true to indicate that we want to close the Socket when we dispose the stream. This time we create a StreamWriter instead of a StreamReader , because we’re now implementing the server, and it’s going to be sending data rather than receiving it. We call the writer’s WriteLine method, passing the current date and time, which, as you may recall, was the whole point of this service in the first place. Example 13-27 shows the completed code. Example 13-27. The complete daytime service using (Socket daytimeListener = new Socket( AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp)) { daytimeListener.SetSocketOption(SocketOptionLevel.IPv6, (SocketOptionName) 27, 0); IPEndPoint daytimeEndpoint = new IPEndPoint(IPAddress.IPv6Any, 13); daytimeListener.Bind(daytimeEndpoint); 534 | Chapter 13: Networking
Image of page 558

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
daytimeListener.Listen(20); while (true) { Socket incomingConnection = daytimeListener.Accept(); using (NetworkStream connectionStream = new NetworkStream(incomingConnection, true)) using (StreamWriter writer = new StreamWriter(connectionStream, Encoding.ASCII)) { writer.WriteLine(DateTime.Now); } } } The first time you run this code, you can expect to see the warning dialog shown in Figure 13-9 (unless you’ve disabled your Windows Firewall). By default, the Windows Firewall will notify you when programs start listening for incoming network connec- tions out of the blue. Typically, a program that has a legitimate need to accept con- nections will register itself with the firewall when it’s installed, so when a program that the firewall knows nothing about suddenly starts listening for incoming connections, that’s likely to be a sign of trouble—it’s exactly the sort of thing that malware would do if it wanted to make your machine available to hackers for distributing spam or launching distributed denial of service attacks. Of course, in this case, you know that the code is legitimate because you just wrote it, and the reason your program hasn’t gone through the official route of registering itself during installation is that you only just wrote the code, and you haven’t written the Windows Installer .msi yet. So as a developer, you expect to see this sort of warning for your own programs when they listen for incoming connections. (You didn’t see this for the WCF example earlier be- cause it was using the specially reserved design-time address space that Visual Studio sets up when you install it. But that works only for HTTP—there’s no equivalent for sockets.) You just need to click Unblock, and you shouldn’t see this warning again for
Image of page 559
Image of page 560
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern