插座ReceiveAsync合并包 [英] Socket ReceiveAsync merging packets

查看:62
本文介绍了插座ReceiveAsync合并包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算通过套接字接收数据包,但因为它们是从发送方以高频率派出了一些人得到打包成一个单一的字节阵列。 SocketAsyncEventArgs.Buffer 则包含多个数据包,即使它们分别发送(使用验证 Wireshark的)。

I intend to receive packets of data over a socket but since they are sent from the sender with a high frequency a number of them get packed into a single byte array. SocketAsyncEventArgs.Buffer then holds multiple packets, even though they were sent separately (verified using wireshark).

我已经尝试排队进入的数据包,异步工作他们,但我仍然得到同样的结果。

I have already tried to queue the incoming packets and asynchronously work them off, but still I get the same result.

可能是什么原因,这种行为?

What could be reason for this behaviour?

推荐答案

这是TCP的工作原理。 TCP连接是一个字节的双向流的,你必须把它看作。从一端单个发送可能导致多读取在接收侧,反之亦然,多发送可能在一个单一的读出结束,与应用消息边界不是由输送pserved $ P $

This is how TCP works. TCP connection is a bi-directional stream of bytes, and you have to treat it as such. Single send from one end might result in multiple reads on the receiving side, and vice versa, multiple sends might end up in a single read, and application message boundaries are not preserved by the transport.

您有,直到你知道你有一个完整的应用程序消息缓冲输入。常见的方法是:

You have to buffer the input until you know you have a complete application message. Common approaches are:


  • 固定长度的消息,

  • pre-待决的消息面前长度

  • 限定的特殊消息结束分离器的流中。

这篇关于插座ReceiveAsync合并包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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