點解server 做緊streaming, NetworkStream.read is blocking?

C#, tcp client..
  1. NetworkStream stream = client.GetStream();
  2. byte[] buffer = new byte[640 * 480];
  3. int read, total = 0;

  4. // Blocking Read here
  5. while ((read = stream.Read(buffer, total, 1000)) != 0)
  6. {
  7.          total += read;
  8. }
複製代碼

https://msdn.microsoft.com/en-us ... .networkstream.aspx

NetworkStream  class 提供的Methods 有  Read 及 ReadAsync 兩種,
當中的Read是synchronous的, 所以 comment 是blocking read.

TOP

Thanks ching

TOP

can check NetworkStream.DataAvailable before read
which can avoid blocking

TOP