title: What is the correct way to read from NetworkStream in .NET
body: <p>I've been struggling with this and can't find a reason why my code is failing to properly read from a TCP server I've also written. I'm using the <code>TcpClient</code> class and its <code>GetStream()</code> method but something is not working as expected. Either the operation blocks indefinitely (the last read operation doesn't timeout as expected), or the data is cropped (for some reason a Read operation returns 0 and exits the loop, perhaps the server is not responding fast enough). These are three attempts at implementing this function:</p>
  
  <pre><code>// this will break from the loop without getting the entire 4804 bytes from the server 
  string SendCmd(string cmd, string ip, int port)
  {
      var client = new TcpClient(ip, port);
      var data = Encoding.GetEncoding(1252).GetBytes(cmd);
      var stm = client.GetStream();
      stm.Write(data, 0, data.Length);
      byte[] resp = new byte[2048];
      var memStream = new MemoryStream();
      int bytes = stm.Read(resp, 0, resp.Length);
      while (bytes &gt; 0)
      {
          memStream.Write(resp, 0, bytes);
          bytes = 0;
          if (stm.DataAvailable)
              bytes = stm.Read(resp, 0, resp.Length);
      }
      return Encoding.GetEncoding(1252).GetString(memStream.ToArray());
  }
  
  // this will block forever. It reads everything but freezes when data is exhausted
  string SendCmd(string cmd, string ip, int port)
  {
      var client = new TcpClient(ip, port);
      var data = Encoding.GetEncoding(1252).GetBytes(cmd);
      var stm = client.GetStream();
      stm.Write(data, 0, data.Length);
      byte[] resp = new byte[2048];
      var memStream = new MemoryStream();
      int bytes = stm.Read(resp, 0, resp.Length);
      while (bytes &gt; 0)
      {
          memStream.Write(resp, 0, bytes);
          bytes = stm.Read(resp, 0, resp.Length);
      }
      return Encoding.GetEncoding(1252).GetString(memStream.ToArray());
  }
  
  // inserting a sleep inside the loop will make everything work perfectly
  string SendCmd(string cmd, string ip, int port)
  {
      var client = new TcpClient(ip, port);
      var data = Encoding.GetEncoding(1252).GetBytes(cmd);
      var stm = client.GetStream();
      stm.Write(data, 0, data.Length);
      byte[] resp = new byte[2048];
      var memStream = new MemoryStream();
      int bytes = stm.Read(resp, 0, resp.Length);
      while (bytes &gt; 0)
      {
          memStream.Write(resp, 0, bytes);
          Thread.Sleep(20);
          bytes = 0;
          if (stm.DataAvailable)
              bytes = stm.Read(resp, 0, resp.Length);
      }
      return Encoding.GetEncoding(1252).GetString(memStream.ToArray());
  }
  </code></pre>
  
  <p>The last one "works", but it certainly looks ugly to put a hard-coded sleep inside the loop considering that sockets already support read timeouts! Do I need to setup some property(ies) on the <code>TcpClient</code> of the <code>NetworkStream</code>? Does the problem resides in the server? The server don't close the connections, it is up to the client to do so. The above is also running inside the UI thread context (test program), maybe it has something to do with that...</p>
  
  <p>Does someone know how to properly use <code>NetworkStream.Read</code> to read data until no more data is available? I guess what I'm wishing for is something like the old Win32 winsock timeout properties... <code>ReadTimeout</code>, etc. It tries to read until the timeout is reached, and then return 0... But it sometimes seem to return 0 when data should be available (or on the way.. can Read return 0 if is available?) and it then blocks indefinitely on the last read when data is not available... </p>
  
  <p>Yes, I'm at a loss!</p>