C代码sendmsg()和recvmsg()通过互联网或内联网上的UDP传输bigdata [英] C code sendmsg() and recvmsg() transfer bigdata over UDP on internet or intranet

查看:593
本文介绍了C代码sendmsg()和recvmsg()通过互联网或内联网上的UDP传输bigdata的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 UDP 发送大于1gb的大数据。我尝试学习 sendmsg()的所有参数recvmsg()但不知道如何使用c编程来分段以及如何重新组装数据。我在谷歌搜索每一方都告诉我如何在 iovec  cmsghdr 中设置一个参数,但是没有人解释如何在另一端分段和重组数据。 

一些例子我发现说发送一个数据缓冲区并根据 sendmsg()的返回值告诉管理偏移但是我不认为这是最好的做的方式。
完成后,我将添加功能广播,所以我只看P.我知道TCP问题变得容易,但我的要求是 UDP
请建议我如何使用c语言对数据进行分段和重组。

和我的英语不太好抱歉。





我的尝试:



 typedef struct header_long_t {
uint32_t magic;
uint32_t msg_seqno;
uint32_t msg_size;
uint32_t fragment_offset;
uint16_t fragment_no;
uint16_t fragments_in_msg;
} header_long_t;





但不是成功。

解决方案

< blockquote>你必须实现自己的协议来处理所有这些情况(碎片,确认,重发丢失数据包等)。



例如,您可以发送包含元信息的起始数据包,例如数据块的总大小和大小。然后使用与您问题中的标题类似的标题以块的形式发送数据。接收器分配结构以知道已接收哪些块(大小=总大小/块大小)。然后,它将每个接收到的块写入指定偏移量的文件,并将该块标记为已接收。这处理碎片。



但是您还需要在超时后识别丢失的块并请求再次发送它们。这可以在任何一方实现​​:接收方可以发送确认,以便发送方可以将块标记为成功发送,或者接收方可以在超时后请求再次发送丢失的块。我会选择确认选项。



您还应该向每个区块发送支票金额。然后接收方可以在检查总和错误时立即请求重新发送。



但是有一个协议可以完成以上所有操作:TCP。



UDP是您的要求的错误选择。与那些想要UDP的人交谈并向他们解释你必须编写类似TCP over UDP的内容来满足要求。这可能比使用TCP慢,而且 - 更重要的是 - 没有大量测试就不那么稳定。


I am trying to send big data more than 1gb over UDP.I try to learn all parameter of sendmsg() and recvmsg() but not get the idea how to fragment and how to again reassemble data using c programming. I search on google every side tell that how to set a parameter in iovec and cmsghdr but no one explain about how to fragment and reassemble data another end.

some example I found that says send a single buffer of data and tell that manage offset based on return value of sendmsg() but I don't think this is the best way of doing.
On completion, I will add feature broadcast so I am only looking P.I know that over TCP problem makes easier but my requirement is UDP. 
please suggest me how to fragment and reassemble data in c language.

and my English not good so sorry for that.



What I have tried:

typedef struct header_long_t {
    uint32_t magic;
    uint32_t msg_seqno;
    uint32_t msg_size;
    uint32_t fragment_offset;
    uint16_t fragment_no;
    uint16_t fragments_in_msg;
}header_long_t;



but not the success.

解决方案

You have to implement your own protocol that handles all these cases (fragmentation, acknowledgement, resending lost packets etc).

You might for example send a start packet that contains the meta information like the total size and the size of data blocks. Then send the data in chunks with a header like the one from your question. The receiver allocates a structure to know which blocks has been received (with size = total size / block size). It then writes each received block to a file at the specified offset and marks the block as received. That handles fragmentation.

But you need also to identify missing blocks after a timeout and request sending those again. This may be implemented on either side: The receiver might send an acknowledgement so that the sender can mark a block as successfully transmitted or the receiver can request sending missing blocks again after a timeout. I would go for the acknowledgement option.

You should also send a check sum with each block. Then the receiver can request resending immediately upon check sum errors.

But there is a protocol that does all the above: TCP.

UDP is the wrong choice for your requirements. Talk to those who want UDP and explain them that you have to write something like TCP over UDP to fullfil the requirements. Such would be probably slower than using TCP and - more important - not so stable without extensive testing.


这篇关于C代码sendmsg()和recvmsg()通过互联网或内联网上的UDP传输bigdata的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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