title: setting NetworkStream.ReceiveTimeout not triggering exception
body: <p>This is a continuation of the <a href="https://stackoverflow.com/questions/7542235/read-specific-number-of-bytes-from-networkstream">this</a> question. I am new to network programming, so I am just writing small sample stuff to gain understanding, but somewhat struggling with explaining results.</p>
  
  <p>It seems setting <code>NetworkStream.ReceiveTimeout</code> is not working correctly when client that was supposed to be sending data simply closes before sending all the expected data.</p>
  
  <p>Here is the sample code:</p>
  
  <pre><code>public static void Main(string[] args)
  {
      TcpListener listener = new TcpListener(IPAddress.Any, 10001);
      listener.Start();
  
      ThreadPool.QueueUserWorkItem(WriterThread);
  
      using (TcpClient client = listener.AcceptTcpClient())
      using (NetworkStream stream = client.GetStream())
      {
          client.ReceiveTimeout = (int)new TimeSpan(0, 0, 2).TotalMilliseconds;
          stream.ReadTimeout = (int)new TimeSpan(0, 0, 2).TotalMilliseconds;
          ReceiveMessage(stream, 1024);
      }
  
      listener.Stop();
  
      Console.WriteLine("Done.");
      Console.ReadKey(true);
  }
  
  
  private static void WriterThread(object state)
  {
      using (TcpClient client = new TcpClient())
      {
          client.Connect(new IPEndPoint(IPAddress.Loopback, 10001));
          using (NetworkStream stream = client.GetStream())
          {
              byte[] bytes = Encoding.ASCII.GetBytes("obviously less than 1024 bytes");
              stream.Write(bytes, 0, bytes.Length);
  
              Thread.Sleep(10000); // comment out 
          }
      }
  }
  
  
  private static byte[] ReceiveMessage(Stream stream, int length)
  {
      byte[] buffer = new byte[length];
      int bufferFill = 0;
  
      while (true)
      {
          bufferFill += stream.Read(buffer, bufferFill, buffer.Length - bufferFill);
          if (buffer.Length == bufferFill)
              return buffer;
  
          Thread.Sleep(100);
      }
  }
  </code></pre>
  
  <p>This version works correctly triggering exception on the <code>stream.Read()</code> call. However If I comment out <code>Thread.Sleep(10000)</code>, the client closes connection, but listener fails to recognize it. Main thread gets stuck inside the <code>while(true)</code> loop. The <code>stream.Read()</code> keeps returning zero, but no exception thrown. </p>
  
  <p>Is this normal? If so how am I expected to handle abnormal client disconnections?</p>