Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send large data on UDP socket

Tags:

qt

udp

I need to send and receive very large data using udp. Unfortunately udp provides 8192 bytes per diagram, so there is need to divide data into smaller pieces. I'm using Qt and QUdpSocket. There is a QByteArray with length of 921600 I want to send to client. I want to send 8192 bytes each time.

What is the fast way to split a QByteArray?

like image 535
sorush-r Avatar asked Dec 01 '11 14:12

sorush-r


People also ask

What is the maximum UDP payload size?

A UDP datagram is carried in a single IP packet and is hence limited to a maximum payload of 65,507 bytes for IPv4 and 65,527 bytes for IPv6. The transmission of large IP packets usually requires IP fragmentation.

Can TCP send data over UDP?

If you're asking if TCP can be implemented in UDP, the answer is no. First, TCP packets and UDP packets have an incompatible format. Second, TCP and UDP have different protocol numbers (seen in the IP header) which means that TCP traffic destined for a UDP port would not be passed to the correct upper-layer protocol.

What is the largest UDP header size?

This field specifies the length in bytes of the UDP header and UDP data. The minimum length is 8 bytes, the length of the header. The field size sets a theoretical limit of 65,535 bytes (8-byte header + 65,527 bytes of data) for a UDP datagram.

Can you transfer files using UDP?

UDP, which stands for User Datagram Protocol, is a method used to transfer large files across the Internet. TCP, or Transmission Control Protocol, is the more widely known and used protocol for file transmission, however, falls short in comparison when it comes to transferring large files at fast speeds.


2 Answers

You should never need to explicitly split the data, just step through it 8 KB at a time. Typically the functions that write data to a socket (including QUdpSocket::writeDatagram, it seems) accept a pointer to the first byte and a byte count, so you can just provide a pointer into the array.

Do note that sending 8 KB datagrams is quite aggressive; they will very likely be fragmented at the IP layer which can affect delivery speed and reliability negatively.

Research the concept of "path MTU", and try to use that for the sends, it might be faster although it will result in more datagrams.

like image 115
unwind Avatar answered Nov 19 '22 01:11

unwind


Actually the length field on a UDP header is 16 bits so a UDP datagram can be up to ~65k (minus the size of the header stuff).

However, as unwind pointed out, it will most likely be fragmented along the way to fit the smallest MTU on the path to the destination.

8192 bytes is the default receive buffer size for Windows operating systems. The MTU is likely 1500 bytes if you are using Ethernet. Any UDP datagram larger than that will be fragmented. If your datagram encounters a device along the route with an even smaller MTU, it will be fragmented yet again.

like image 41
Dave Rager Avatar answered Nov 19 '22 00:11

Dave Rager