Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Updated 18-Jul-17 1:25am

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900