需要 NetworkStream.Read 行为的说明
我使用阻塞读取来消耗来自网络流的实时数据,如下所示:
Do
NetworkStream.Read(Bytes, 0, ReceiveBufferSize)
'Do stuff with data here
Loop
在 Wireshark 中观察网络中传入的数据包,我发现有时当新数据包传入时,.NET 会立即看到它并解除阻塞,让我进行处理它。其他时候,在 NetworkStream.Read 解锁并一次性返回全部数据之前,多个数据包会进入线路 - 在 NetworkStream 读取解锁之前,我已经看到多达 8 个数据包缓冲区。
这是预期的行为吗?有没有办法在通过网络接收到每个数据包时立即抓取并处理该数据包?异步接收模型在这里会有什么不同吗?或者我只是从根本上误解了 TCP 流的工作方式?
I am consuming real-time data from a network stream using a blocking read as follows:
Do
NetworkStream.Read(Bytes, 0, ReceiveBufferSize)
'Do stuff with data here
Loop
Watching packets come in on the wire in Wireshark, I see that sometimes when a new packet comes in, .NET sees it immediately and unblocks, letting me process it. Other times, multiple packets will come in on the wire before the NetworkStream.Read unblocks and returns the whole lot in one go - I've seen up to 8 packets buffer before the NetworkStream read unblocks.
Is this expected behaviour? Is there a way to grab and process each packet immediately as it is received across the wire? Will an Async receive model make any difference here? Or am I just fundamentally misunderstanding the way that TCP streams work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论