PETR
programming c# 4.0.pdf

As you can see from the last line of listen takes a

Info icon This preview shows pages 557–559. Sign up to view the full content.

As you can see from the last line of Example 13-25 , Listen takes a single argument. This indicates the maximum backlog for this socket. The backlog allows for the situa- tion where new connections arrive faster than our server can handle them. As you’ll see shortly, we need to execute some code to accept each incoming connection, and at busy times, we might lag behind—if a new connection request comes in before we’ve managed to accept the last one, that new request goes into the backlog queue. If the number of requests in the backlog gets as high as the number we pass to Listen , the OS will start rejecting any further requests until our application catches up. Our socket is now in a listening state, which means that if client programs start trying to connect to our computer on port 13, the OS knows those connections are destined for our program. The next thing our code has to do is accept those connections. Ex- ample 13-26 does this in a loop so that it can keep accepting connection requests for as long as the program runs. Example 13-26. Accepting incoming connections while (true) { Socket incomingConnection = daytimeListener.Accept(); using (NetworkStream connectionStream = new NetworkStream(incomingConnection, true)) using (StreamWriter writer = new StreamWriter(connectionStream)) { writer.WriteLine(DateTime.Now); } } This code calls Accept on the listening Socket . If there are currently no clients trying to connect to the service, this call will block—it won’t return until there’s a client. Once Sockets | 533
Image of page 557

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

at least one client is attempting to use the service, this will return, handing back another Socket object. The Socket API is designed to allow multiple simultaneous connections to a single service, and so each call to Accept returns a new Socket . Your server will end up with one Socket object for each distinct connected client, plus the one listening Socket . You never actually send or receive data on the listening socket. It doesn’t represent a TCP connection—its only job is to return a new socket for each incoming TCP connection you accept. Arguably it’s a little weird to use the same Socket class for both jobs, because accepting incoming connections feels like a pretty different kind of activity than representing an active TCP connection. But that’s how sockets have worked for dec- ades. .NET is merely continuing the slightly eccentric tradition. Example 13-26 chooses to deal with the clients one at a time—the loop accepts a single connection, sends a response, closes the connection, and then moves on to the next client. So this particular server will have up to two active Socket objects at any one time—the one for the client connection it’s currently handling, and the one Socket that is listening for incoming connections. You don’t need to do this—it’s very common to accept new connections on a listening socket when you already have open connections that came from the same socket. (For example, a web server does not insist on finishing
Image of page 558
Image of page 559
This is the end of the preview. Sign up to access the rest of the document.
  • Spring '15

{[ 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