简体   繁体   中英

how to fragment packets in c++?

I have been implementing the MSEX CITP protocol - with success so far - in my project for streaming image over the network. I'm using winsocks like so:

v_id = socket(AF_INET, SOCK_DGRAM, 0);
setsockopt(v_id, SOL_SOCKET, SO_BROADCAST, (char*)&isbroad, sizeof(isbroad));
sendto(v_id, temp_buf, v_buffer->o(), 0, address->get(), socksize);

But for image larger than 65k, the spec says that I have to fragment my packets and add a given "preamble".

After some research, from what I understand, I have to set MTU size and fragment header but all my attempts are failing. Can someone point me in the right direction?

MTU means Maximum Transmission Unit : it represente the size of the biggest PDU (protocol data unit) that can be sended/received in a one network transaction (generally 1500 bytes for Ethernet).

In your case, it is clear that the size of your data will greatly exceeds the size of MTU. You must therefore send your files in several fragments.

This constraint forces you to explicitly manage:

1. The sending/receiving of fragments
2. The reconstruction of the original file by concatenating its different fragments in the correct order

To identify and manage your fragments, you must add headers as meta-data to your packets. This headers contain for example the size and the sequence number of the fragment:

---------------------------  
| headers |       data    |
---------------------------  

Thanks to these headers, you will know the size of data to read, the sequence number of the fragment. What will allow you to rebuild your image.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM