title: Method to receive all bytes from a message-framed stream freezes
body: <p>Recently, I studied Message Framing in my own time and decided that I should use the common solution of prepending the message with the length of the actual message. I use the Tcp wrappers for sockets (TcpListener, TcpClient).</p>
  
  <p><strong>Context:</strong>
  Both server and client communicate with base64 encoded serialized objects. On networks with low traffic, the complete data seems to reach its destination in one piece, and can be decoded and then deserialized. However, on networks with high traffic, or low resource availability, I always get a FormatException that the data is not in a proper Base64 format (or similar errors). Here is the method for sending a message:</p>
  
  <pre><code>    public void Send(Packet packData)
      {
          try
          {
              StreamWriter w = new StreamWriter(clientObject.GetStream());
              string s = SerializeData(packData);
              w.WriteLine(s.Length.ToString() + s);
              w.Flush();
          }
          catch (NullReferenceException) { }
          catch (IOException) { CloseObject(); }
      }
  </code></pre>
  
  <p>Now, here is the method I devised for reading the stream on the receiving end. It appears to freeze at the while loop:</p>
  
  <pre><code>    private string ReceiveAllDataFromStream(StreamReader r)
      {
          int a = 0;
          int b = 0;
          string str = null;
          try
          {
              str = r.ReadLine();
              // find out how many chars are integers, then get the value in int. (this is the length of the message)
              foreach (char f in str) { if (int.TryParse(f.ToString(), out a)) { b++; } else { break; } }
              // reuse "a", it doesn't actually contain the result of the TryParse method called above
              a = int.Parse(str.Substring(0, b));
              // remove these chars (the ints) from the message
              str = str.Remove(0, b);
              // calculate how many chars need to be received to complete this message
              int msgRemaining = a - str.Length;
              int msgAcquired = str.Length;
              if (msgAcquired != a)
              {
                  // where i believe the freeze is caused
                  while (msgAcquired != a)
                  {
                      // make a call to supposedly retreieve these last few chars
                      string s = r.ReadLine();
                      // insert as much of the new string received as "msgRemaining" demands
                      str.Insert(0, s.Substring(0, msgRemaining));
                      // add the total length of the inserted string to the amount already aquired, continue with loop
                      msgAcquired = msgAcquired + s.Substring(0, msgRemaining).Length;
                  }
              } return str;
          }
          catch (FormatException) { System.Windows.Forms.MessageBox.Show(b.ToString() + "|" + str.Substring(0, b)); return null; }
      }
  </code></pre>
  
  <p>When I say it freezes, I mean the application goes into an idle state (UI isn't blocked, but I believe that's because this method is running on another thread). What is wrong with my method, and how do I go about fixing it?</p>
  
  <p>Thank you.</p>