[Development] There is the wrong behavior with QUdpSocket && EAGAIN on *nix?

Thiago Macieira thiago.macieira at intel.com
Wed Mar 1 21:17:39 CET 2017

Em quarta-feira, 1 de março de 2017, às 02:27:54 PST, Denis Shienkov escreveu:
> Hi all,
> I have use Qt 5.8, and I want to send to the UDP socket many datagrams
> (e.g. 10000 datagrams, each datagram have 1000 bytes size).
> I use following code:
>         int busyCounter = 0;
>         for(;;) {
>             ...
>             const QNetworkDatagram datagram(data, m_remoteAddress,
> Protocol::SlavePort);
>             if (m_dataSocket->writeDatagram(datagram) == -2) {
>                 QElapsedTimer timer;
>                 timer.start();
>                 const bool result = m_dataSocket->waitForBytesWritten(-1);

This isn't needed and isn't useful. QAbstractSocket::waitForBytesWritten has 

    if (writeBuffer.isEmpty())
        return false;

Since QUdpSocket is unbuffered, the write buffer is always empty. So 
waitForBytesWritten will always return false.

> As I understand, when I got the EAGAIN error (when the writeDatagram()
> method fails
> with the error code == -2), it means, that the transmit queue is full, and
> I need wait
> for something time, e.g. using the select() function:
> https://linux.die.net/man/2/sendmsg

Which is QSocketNotifier.

QUdpSocket is unbuffered, so the bytesWritten() signal is not useful. We can't 
add a readyWrite() signal because it would be fired all the time. You'll have 
to do the QSocketNotifier.

...unless QUdpSocket already creates one on that socket. The event dispatcher 
does not allow two QSN on the same socket, with the same type. If that 
happens, let us know.

> and then, as I understand, I need try to send same datagram again.
> Instead of the select() I use the QUdpSocket::waitForBytesWritten() method,
> which uses the select/pool/epoll internally.

You don't get to poll(). See above.

Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel Open Source Technology Center

More information about the Development mailing list