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

Konstantin Tokarev annulen at yandex.ru
Thu Mar 2 08:33:34 CET 2017



01.03.2017, 13:28, "Denis Shienkov" <denis.shienkov at gmail.com>:
> 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).

Totally unrelated to your main issue, but make sure you are using connected UDP
socket when sending many datagrams to same host, it should improve performance

>
> 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);
>                 ++busyCounter;
>                 qDebug() << "Wait result:" << result << "nsecs:" << timer.nsecsElapsed() << "busy counter:" << busyCounter;
>             } else {
>                 busyCounter = 0;
>                 break;
>             }
>         }
>
> 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
>
> "
> When the message does not fit into the send buffer of the socket, send() normally blocks, unless the socket has been placed in nonblocking I/O mode. In nonblocking mode it would fail with the error EAGAIN or EWOULDBLOCK in this case. The select(2) call may be used to determine when it is possible to send more data.
> "
>
> 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.
>
> But, seems it does not work, as described in the POSIX documentation,
> I got following debug output:
>
> "
> Wait result: false nsecs: 363 busy counter: 1
> ...
> Wait result: false nsecs: 140 busy counter: 232
> ...
> Wait result: false nsecs: 167 busy counter: 1
> ...
> Wait result: false nsecs: 139 busy counter: 238
> ...
> Wait result: false nsecs: 167 busy counter: 1
> ..
> Wait result: false nsecs: 167 busy counter: 19
> ...
> "
>
> and etc.
>
> I suspect, that the busyCounter always should be 1, but it does
> not happens, and the waitForBytesWritten() always fails!
>
> What doing wrong?
>
> BR,
> Denis
>
> ,
>
> _______________________________________________
> Development mailing list
> Development at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/development


-- 
Regards,
Konstantin



More information about the Development mailing list