TcpClient.GetStream()。DataAvailable返回false,但流有更多的数据 [英] TcpClient.GetStream().DataAvailable returns false, but stream has more data

查看:2571
本文介绍了TcpClient.GetStream()。DataAvailable返回false,但流有更多的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此​​,它似乎是一个阻塞read()方法可以返回,这样做是接收所有数据被发送到它。反过来,我们与由从有问题的数据流的DataAvailable值控制循环换读()。问题是,你可以,而在这个while循环接收更多的数据,但没有在幕后处理事情让系统知道这一点。大多数在网上我发现这个解决方案在这种或那种方式都没有适用于我。

So, it would seem that a blocking Read() can return before it is done receiving all of the data being sent to it. In turn we wrap the Read() with a loop that is controlled by the DataAvailable value from the stream in question. The problem is that you can receive more data while in this while loop, but there is no behind the scenes processing going on to let the system know this. Most of the solutions I have found to this on the net have not been applicable in one way or another to me.

我已经结束了做的是在我的循环的最后一步,我从流中读取每个数据块后,做了一个简单的Thread.Sleep(1)。这似乎给系统时间更新和我没有得到准确的结果,但是这似乎有点哈克和相当多的'间接'的解决方案。

What I have ended up doing is as the last step in my loop, I do a simple Thread.Sleep(1) after reading each block from the stream. This appears to give the system time to update and I am not getting accurate results but this seems a bit hacky and quite a bit 'circumstantial' for a solution.

下面是我处理的情况列表:IIS应用程序和独立的应用程序,无论是用C#编写的发送之间单一的TCP连接/接收通信。它发送一个请求,然后等待响应。这个请求是通过HTTP请求发起的,但我没有这个问题从HTTP请求中读取数据,这是事实了。

Here is a list of the circumstances I am dealing with: Single TCP Connection between an IIS Application and a standalone application, both written in C# for send/receive communication. It sends a request and then waits for a response. This request is initiated by an HTTP request, but I am not having this issue reading data from the HTTP Request, it is after the fact.

下面是基本的code用于处理传入的连接。

Here is the basic code for handling an incoming connection

protected void OnClientCommunication(TcpClient oClient)
{
    NetworkStream stream = oClient.GetStream();
    MemoryStream msIn = new MemoryStream();

    byte[] aMessage = new byte[4096];
    int iBytesRead = 0;

    while ( stream.DataAvailable )
    {
        int iRead = stream.Read(aMessage, 0, aMessage.Length);
        iBytesRead += iRead;
        msIn.Write(aMessage, 0, iRead);
        Thread.Sleep(1);
    }
    MemoryStream msOut = new MemoryStream();

    // .. Do some processing adding data to the msOut stream

    msOut.WriteTo(stream);
    stream.Flush();

    oClient.Close();
}

所有反馈欢迎一个更好的解决方案,或只是需要给在休眠(1)一展身手,让我们检查DataAvailable值之前的事情正确更新竖起了大拇指。

All feedback welcome for a better solution or just a thumbs up on needing to give that Sleep(1) a go to allow things to update properly before we check the DataAvailable value.

猜猜我是2年后希望的答案这问题是没怎么事情还包括:)

Guess I am hoping after 2 years that the answer to this question isn't how things still are :)

推荐答案

我看到这个问题。结果
你期待的沟通会比而()循环,这是不太可能更快。结果
而()循环将尽快有没有更多的数据,这可能不是退出几毫秒刚过的情况下完成。

I'm seeing a problem with this.
You're expecting that the communication will be faster than the while() loop, which is very unlikely.
The while() loop will finish as soon as there is no more data, which may not be the case a few milliseconds just after it exits.

您期望一定量的字节?结果
如何往往是 OnClientCommunication()解雇?谁触发的?

Are you expecting a certain amount of bytes?
How often is OnClientCommunication() fired? Who triggers it?

你有什么用而()循环后的数据呢?你一直追加到previous数据?

What do you do with the data after the while() loop? Do you keep appending to previous data?

DataAvailable 返回假的,因为你正在阅读比通信速度更快,所以这是精唯一,如果你继续回来到这个code块来处理更多的数据来了。

DataAvailable WILL return false because you're reading faster than the communication, so that's fine only if you keep coming back to this code block to process more data coming in.

这篇关于TcpClient.GetStream()。DataAvailable返回false,但流有更多的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆