[Interest] Proper way to handle QUdpSocket::writeDatagram when it returns TemporaryError
thiago.macieira at intel.com
Wed Oct 9 08:44:02 CEST 2019
On Tuesday, 8 October 2019 14:09:18 PDT Ola Røer Thorsen wrote:
> Hi all, I've got a thread in an application (Linux, Qt 5.13.0) which just
> writes udp datagrams at a relatively high frequency (video streaming).
> Sometimes the QUdpSocket::writeDatagram function returns -1 and the socket
> error is "TemporaryError". Looking into the source code of the udp socket
> class, it seems like the actual reason is that the OS returns EAGAIN which
> means I need to try again.
That sounds about right: if you exceed the buffering limits in the kernel, it
won't take more data from you. And QUdpSocket does not buffer either.
> What's the best way to handle this? Right now I just made a dumb loop
> trying again and again with a short usleep inbetween until it finally ships
> the datagram. I tried using waitForBytesWritten when getting the temporary
> error before trying again, but that had no noticeable effect. Without a
> sleep, this spinning loop might have some several 100 retries before it is
The only way to handle this is to make sure your protocol reduces the
bandwidth it's trying to consume. Don't try to retransmit the same thing over
and over again. Design your code so that you go to a lower transfer rate if
you start getting TemporaryErrors.
waitForBytesWritten() does not help because there's nothing to be written. As
I said, QUdpSocket does not buffer, so all bytes have already been written.
Thiago Macieira - thiago.macieira (AT) intel.com
Software Architect - Intel System Software Products
More information about the Interest