How to split data to fit in UDP datagram while using Network.framework?

I have a data that should be transmitted using UDP with NWConnection.

Size of data is larger than maximum datagram size provided by UDP, so I need to split data to send it.

I tried to find solution with searching about UDP data split, but couldn't find appropriate solution to my problem.

Which keyword should I search about to start from?

Answered by Systems Engineer in 715097022

I need to split data to send it. I tried to find solution with searching about UDP data split, but couldn't find appropriate solution to my problem.

TCP or UDP data that is transmitted on a connection can be done so using a framer to keep track of the size of the transmission. A very common framer is TLV, or type, length, value. A great example of this is built into the sample for Building a Custom Peer-to-Peer Protocol. Now, the example here uses TCP, but the same logic for UDP can be used if desired. Keep in mind that the idea here is to fit your UDP messages into custom frames and send them so they can always be re-assembled the same way on the other side:

(TYPE) - Byte 01 => 1 at index: 0 (type of packet) 
(TYPE) - Byte 00 => 0 at index: 1 
(TYPE) - Byte 00 => 0 at index: 2 
(TYPE) - Byte 00 => 0 at index: 3 
(LENGTH) - Byte 0B => 11 at index: 4 (length of packet) 
(LENGTH) - Byte 00 => 0 at index: 5 
(LENGTH) - Byte 00 => 0 at index: 6 
(LENGTH) - Byte 00 => 0 at index: 7 
(VALUE) - Byte 54 => T at index: 8  ( bytes of payload in the packet) 
(VALUE) - Byte 45 => E at index: 9 
(VALUE) - Byte 53 => S at index: 10 
(VALUE) - Byte 54 => T at index: 11 
(VALUE) - Byte 49 => I at index: 12 
(VALUE) - Byte 4E => N at index: 13 
(VALUE) - Byte 47 => G at index: 14 
(VALUE) - Byte 44 => D at index: 15 
(VALUE) - Byte 41 => A at index: 16 
(VALUE) - Byte 54 => T at index: 17 
(VALUE) - Byte 41 => A at index: 18 
Accepted Answer

I need to split data to send it. I tried to find solution with searching about UDP data split, but couldn't find appropriate solution to my problem.

TCP or UDP data that is transmitted on a connection can be done so using a framer to keep track of the size of the transmission. A very common framer is TLV, or type, length, value. A great example of this is built into the sample for Building a Custom Peer-to-Peer Protocol. Now, the example here uses TCP, but the same logic for UDP can be used if desired. Keep in mind that the idea here is to fit your UDP messages into custom frames and send them so they can always be re-assembled the same way on the other side:

(TYPE) - Byte 01 => 1 at index: 0 (type of packet) 
(TYPE) - Byte 00 => 0 at index: 1 
(TYPE) - Byte 00 => 0 at index: 2 
(TYPE) - Byte 00 => 0 at index: 3 
(LENGTH) - Byte 0B => 11 at index: 4 (length of packet) 
(LENGTH) - Byte 00 => 0 at index: 5 
(LENGTH) - Byte 00 => 0 at index: 6 
(LENGTH) - Byte 00 => 0 at index: 7 
(VALUE) - Byte 54 => T at index: 8  ( bytes of payload in the packet) 
(VALUE) - Byte 45 => E at index: 9 
(VALUE) - Byte 53 => S at index: 10 
(VALUE) - Byte 54 => T at index: 11 
(VALUE) - Byte 49 => I at index: 12 
(VALUE) - Byte 4E => N at index: 13 
(VALUE) - Byte 47 => G at index: 14 
(VALUE) - Byte 44 => D at index: 15 
(VALUE) - Byte 41 => A at index: 16 
(VALUE) - Byte 54 => T at index: 17 
(VALUE) - Byte 41 => A at index: 18 
How to split data to fit in UDP datagram while using Network.framework?
 
 
Q