C# read all bytes

17,087

Solution 1

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.

Solution 2

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());
  }
}
Share:
17,087
jadrijan
Author by

jadrijan

Software Engineer

Updated on June 15, 2022

Comments

  • jadrijan
    jadrijan almost 2 years

    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
    jadrijan about 10 years
    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?
  • usr
    usr about 10 years
    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.
  • jadrijan
    jadrijan about 10 years
    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?
  • usr
    usr about 10 years
    @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).
  • ChrisB
    ChrisB over 6 years
    Change to while loop to an do-while loop for an upvote :) (stackoverflow.com/a/12025673/8760418)