当 DataAvailable 为 false 时,确定从 NetworkStream 中读取所有数据吗?
还是数据的发送者必须先发送数据的长度。我必须阅读,直到我阅读了发送者指定的字节数?
样品:
private Byte[] ReadStream(NetworkStream ns)
{
var bl = new List<Byte>();
var receivedBytes = new Byte[128];
while (ns.DataAvailable)
{
var bytesRead = ns.Read(receivedBytes, 0, receivedBytes.Length);
if (bytesRead == receivedBytes.Length)
bl.AddRange(receivedBytes);
else
bl.AddRange(receivedBytes.Take(bytesRead));
}
return bl.ToArray();
}