This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

cc3200 udp调用recvfrom收包出现粘包

Other Parts Discussed in Thread: CC3200

我现在测试udp的应用,在设备上使用select和recvfrom来接收消息。在linux上运行一个udp应用,调用sendto不停的往设备发数据。

现在问题是,在设备上recvfrom接收到的数据出现混乱,并不是按packet来收的。比如一次收到2个包,或者1.5个包。

附件是我的代码:

3200代码运行在TI的开发板上,3200_main.c为测试代码,对应的log文件为3200.log

linux_udp_client.c为linux上的测试代码,对应的log文件为linux_test.log

tcpdump.cap为linux端抓取的网络数据包

udp_recvfrom_issue_1713.zip

  • 你可能需要加一些间隔,CC3200的UDP速度为:
    Application Throughput
    UDP: 16 Mbps
    TCP: 13 Mbps
  • hi Alvin

    您的意思是在发送端添加发送间隔吗?可是发送端代码我们是不可控的。

    同时我还有一个疑惑,就是udp数据不是以packet来接收的吗?现在我在发送端发送一个100byte的packet,在接收端却可以调用10次recvfrom,每次10byte的方式来接受这个packet,我疑惑的是udp应该是recvfrom会处理一个完整的packet,如果recvfrom传参的buflen小于当前packet的size,则会截取buflen长度数据到buf,剩余的数据会丢弃。不知道我理解是否有问题。

  • 你把你linux的 udp 设置为堵塞模式试试,

    建议你参考cc3200-sdk\example\udp_socket
    里面的udp test len 为1400 没有问题,

    你后面的理解时对的,
    CC3200 udp len:

    Specifies the length in bytes of the buffer pointed to by the buffer argument. Range: 1-16000 bytes
    dev.ti.com/.../group___socket.html
  • 问题已解决,添加以下两行代码,防止udp包被打乱
    _u8 RxAggrEnable = 0;
    sl_NetCfgSet(SL_SET_HOST_RX_AGGR, 0, sizeof(RxAggrEnable), (_u8*)&RxAggrEnable);
  • RxAggrEnable 是什么用的
  • 当你使能时只有收到定长的字节才能返回,disable 后则可以返回任意长度。
    When you want to get data of length X from the device, and this feature is enabled, it would return only when all X bytes are received (the loop for recvfrom() is done in the device, not in host side). When this feature is disabled, it would return with whatever length it has. So if 2 packets already received, both would be concatenated and transferred to the host.
  • 我也不清楚是干什么用的,当时是代理商给的回复
  • 意思是我disable后,反倒可能导致粘包咯。可能是现在包间隔较大,还没出现粘包的问题。

    When you want to get data of length X from the device, and this feature is enabled, it would return only when all X bytes are received (the loop for recvfrom() is done in the device, not in host side).
    我想请问下这种情况下的recvfrom怎么使用,在调用recvfrom的时候根本不知道对方会传多少字节过来啊。