Note that we dont specify where were connecting to

Info icon This preview shows pages 553–555. Sign up to view the full content.

View Full Document Right Arrow Icon
Note that we don’t specify where we’re connecting to yet. That information doesn’t go in the constructor because not all sockets work the same way—some protocols support transmission patterns other than simple point-to-point connections. So the Socket class requires that we first say what sort of socket we want before going on to say what we’re trying to communicate with. We supply that information when we connect to the service: daytimeSocket.Connect(serverIp, 13); Remember, port 13 is the port number allocated by IANA for the daytime service. We’re going to retrieve the time of day as text from this service, so we declare a variable to hold the result: string data; Sockets represent all data as bytes. (Or more precisely, octets , which are 8-bit bytes. Back in the old days, some computers used other byte sizes, and you occasionally come across evidence of this—for example, some parts of the Internet email system guarantee to transfer 8-bit bytes, and may truncate your data to seven bits per byte.) The Daytime Protocol specification says that the service will return text using the ASCII encoding, so we need something that can convert a stream of bytes containing ASCII into a .NET string. Example 13-23 does this. Example 13-23. Retrieving ASCII data from a TCP socket using (Stream timeServiceStream = new NetworkStream(daytimeSocket, true)) using (StreamReader timeServiceReader = new StreamReader(timeServiceStream, Encoding.ASCII)) { data = timeServiceReader.ReadToEnd(); } A few things are going on here. First, we constructed a NetworkStream —this class derives from Stream , and it’s how .NET lets us treat a socket-based connection in the same way as any other Stream . In general, the use of streams is optional because the Socket class provides methods that let you read and write data directly. But in this example, getting an actual Stream object is useful because we can plug it into a StreamReader . Stream Reader takes a stream that contains text and can convert the bytes in that stream into string objects. Example 13-23 uses the StreamReader class’s ReadToEnd method—this asks to read all of the data in the stream to the very end and to return it as a single string. Sockets | 529
Image of page 553

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

View Full Document Right Arrow Icon
Socket Read Granularity Beware of a classic rookie mistake with TCP sockets. Developers often observe that if they write, say, 20 bytes into a socket, and then on the receiving end they perform a read operation that asks for more bytes (e.g., 1,000), that read usually returns 20 bytes rather than waiting for the requested number of bytes to arrive. Many people mistakenly assume this means TCP guarantees that data will arrive in chunks of the same size in which it was sent. In practice, if a client sends a 20-byte chunk of data, the receiving end may well return six bytes of that chunk from the first read, then another 13 in the next, and then the last byte in the next read. Or even better, it might decide to aggregate that final byte onto the front of the next lump of data sent by the client.
Image of page 554
Image of page 555
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