繁体   English   中英

C#NetworkStream.Read读取的内容比指定的多

[英]C# NetworkStream.Read reads more than specified

在包装上40-50,程序读取比指定的(温度)大2-4个字节,这可能是什么错误?

size =  nsgsout.Read(buf, 0, 2);

while (size != 2)
{
  size += nsgsout.Read(buf, size, 2 - size);
}

temp = (buf[0] + buf[1] * 256);
size = nsgsout.Read(buf, 2, temp - 2);

while (size != temp - 2)
{
    size += nsgsout.Read(buf, size + 2, temp - size + 2);
}  

我认为这并没有您认为的那样:

temp - size+2

我怀疑您期望它的意思是:

temp - (size + 2)

但这实际上相当于

(temp - size) + 2

我怀疑您真的希望电话是:

size += nsgsout.Read(buf, size + 2, temp - size - 2);

另请注意,您可以更改此设置:

size = nsgsout.Read(buf, 2, temp - 2);

只是

size = 0;

然后进入循环,让第一个也阅读...

2尺寸可以是负数。 您可能需要阅读尺码-改为2?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM