简体   繁体   English

NetworkStream.Read()在第一次循环迭代后无法正确读取

[英]NetworkStream.Read() not correctly reading after first iteration of loop

I'm having a problem where my code works as expected when I'm stepping through it but will read incorrect data when running normally. 我遇到了一个问题,我的代码在逐步执行时会按预期工作,但是在正常运行时会读取不正确的数据。 I thought the problem could have been timing however NetworkStream.Read() should be blocking and I also tested this by putting the thread to sleep for 1000ms (more than enough time and more time than I was giving whilst stepping through). 我以为问题可能出在时间上,但是NetworkStream.Read()应该被阻塞,我还通过将线程休眠1000毫秒 (这比遍历时要花的时间和更多的时间)进行了测试。

The purpose of the code (and what is does when stepping through) is to read a bitmap image into a buffer that is preceded by a string containing the image size in bytes followed by a carriage return and a new line. 该代码的目的(以及逐步执行的操作)是将位图图像读入缓冲区,该缓冲区前面是一个字符串,其中包含以字节为单位的图像大小,后跟回车符和换行符。 I believe the problem lies in the read statements, but I can't be sure. 我相信问题出在读取语句上,但是我不确定。 The following code is contained within a larger loop also containing Telnet reads, however I have not has a problem with those and they are only reading ASCII strings, no binary data. 以下代码包含在一个更大的循环中,该循环也包含Telnet读取,但是我没有遇到任何问题,它们仅读取ASCII字符串,没有二进制数据。

List<byte> len = new List<byte>();
byte[] b = new byte[2];
while (!Encoding.ASCII.GetString(b).Equals("\r\n"))
{
    len.Add(b[0]);
    b[0] = b[1];
    b[1] = (byte)stream.ReadByte();
}
len = len.FindAll(x => x != 0);
len.Add((byte)0);
string lenStr = Encoding.ASCII.GetString(len.ToArray());
int imageSize = int.Parse(lenStr);
byte[] imageIn = new byte[imageSize];
stream.Read(imageIn, 0, imageSize);
using (MemoryStream g = new MemoryStream(imageIn))
{
    g.Position = 0;
    bmp = (Bitmap)Image.FromStream(g);
}

The actual problem that occurs with the code is that the first time it runs it correctly receives the length and image, however it does not seem to recognize the \\r\\n in consecutive reads, however this may only be a symptom and not the problem itself. 该代码出现的实际问题是,它第一次运行时正确接收了长度和图像,但是在连续读取中似乎无法识别\\r\\n ,但是这可能只是一种症状,而不是问题。本身。

Thanks in advance! 提前致谢!

EDIT: So I did narrow the problem down and manage to fix it by adding in some artificial delay between my Telnet call using NetworkStream.Write() to retrieve the image and the networkStream.Read() to retrieve it, however this solution is messy and I would still like to know why this issue is happening 编辑:所以我确实缩小了问题的范围,并设法通过在使用NetworkStream.Write()检索图像和networkStream.Read()进行图像检索的Telnet调用之间添加一些人为的延迟来解决问题,但是这种解决方案很麻烦而且我仍然想知道为什么发生此问题

The Read() operation returns the number of bytes actually read. Read()操作返回实际读取的字节数。 It only blocks when there is no data to read, and it can return less bytes as specified by the count parameter. 它仅在没有要读取的数据时才阻塞,并且可以返回较少的字节(如count参数所指定)。

You can easily fix this by putting this inside a loop: 您可以通过将其放入循环中来轻松解决此问题:

byte[] imageIn = new byte[imageSize];
int remaining = imageSize;
int offset = 0;
while (remaining > 0)
{
    int read = stream.Read(imageIn, offset, remaining);
    if (read == 0) throw new Exception("Connection closed before expected data was read");
    offset += read;
    remaining -= read;
}

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

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