2

I am trying to write a simple client/server application in C#. The following is an example server reply sent to my client:

reply {20}<entry name="test"/>

where {20} indicates number of chars that full reply contains. In the code I wrote below how can I use this number to loop and read ALL chars?

TcpClient tcpClient = new TcpClient(host, port);

NetworkStream networkStream = tcpClient.GetStream();

...

// Server Reply
if (networkStream.CanRead)
{
    // Buffer to store the response bytes.
    byte[] readBuffer = new byte[tcpClient.ReceiveBufferSize];

    // String that will contain full server reply
    StringBuilder fullServerReply = new StringBuilder();

    int numberOfBytesRead = 0;

    do
    {
        numberOfBytesRead = networkStream.Read(readBuffer, 0, readBuffer.Length);
        fullServerReply.AppendFormat("{0}", Encoding.UTF8.GetString(readBuffer, 0, tcpClient.ReceiveBufferSize));
    } while (networkStream.DataAvailable);
}
jadrijan
  • 1,438
  • 4
  • 31
  • 48

2 Answers2

10

You're not using numberOfBytesRead. It is fascinating to me that every 2nd TCP question has this same issue as its answer.

Apart from that, you cannot split UTF-8 encoded string at arbitrary boundaries. Encoding.UTF8.GetString will return garbage. Use StreamReader.

usr
  • 168,620
  • 35
  • 240
  • 369
  • I made all the changes you recommended but I am still unable to receive all data (ex. a reply containing 50000 chars). If I modify the byte[tcpClient.ReceiveBufferSize] to byte[10485760] then I get all. How do I get all data when I do not know how much data there is? – jadrijan Mar 18 '14 at 18:42
  • 1
    You read until get read zero bytes. That's the end of the stream. by convention, all streams work that way. This is no different than when reading from a file. Be aware that you'll receive the data in arbitrary chunks. – usr Mar 18 '14 at 18:44
  • Sorry for my silly questions, I've never done this before. How do I know that there is a next chunk, and how do I get the next chunk? – jadrijan Mar 18 '14 at 18:49
  • 1
    @jadrijan you get the next chunk by just reading. Knowing whether there is another is harder. In your case you know how long the message is ("`{20}`"). Read until you have read exactly this many bytes. Don't read less or more. Alternatively, if you know that the remote side will close the connection after having sent all data you can just read until Read returns 0 (the end of the stream). – usr Mar 18 '14 at 18:52
7

The code is just horribly wrong. @usr already pinpointed two big mistakes.

Here is corrected code:

// Server Reply
if (networkStream.CanRead) {
  // Buffer to store the response bytes.
  byte[] readBuffer = new byte[tcpClient.ReceiveBufferSize];
  string fullServerReply = null;
  using (var writer = new MemoryStream()) {
    while (networkStream.DataAvailable) {
      int numberOfBytesRead = networkStream.Read(readBuffer, 0, readBuffer.Length);
      if (numberOfBytesRead <= 0) {
        break;
      }
      writer.Write(readBuffer, 0, numberOfBytesRead);
    }
    fullServerReply = Encoding.UTF8.GetString(writer.ToArray());
  }
}
Ondrej Svejdar
  • 21,349
  • 5
  • 54
  • 89
  • Change to while loop to an do-while loop for an upvote :) (https://stackoverflow.com/a/12025673/8760418) – ChrisB Oct 12 '17 at 18:26