TcpClient.GetStream().DataAvailable returns false, but stream has more data

后端 未结 5 2068
天涯浪人
天涯浪人 2020-12-05 15:50

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 b

5条回答
  •  悲哀的现实
    2020-12-05 16:34

    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.

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

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

    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.

提交回复
热议问题