[英]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.