From 10:00 CET Friday 22nd November we will adjust how the server works to deal with some recently reported problems. Therefore there may be a load problem, if you experience more problems than usual trying to access the forum then please PM AndyS or any of the moderators so they can inform me.


QUDPsocket paradigma looks confusing



  • This code taken from the net looks easy to understand. But I doubt that!

    connect(&udpSocket, SIGNAL(readyRead()),
    this, SLOT(processPendingDatagrams()))

    void WeatherStation::processPendingDatagrams()
    {
    QByteArray datagram;

    do {
    datagram.resize(udpSocket.pendingDatagramSize());
    udpSocket.readDatagram(datagram.data(), datagram.size());
    } while (udpSocket.hasPendingDatagrams());

    etc

    }

    Basically while (udpSocket.hasPendingDatagrams()) looks a bit odd. Let say that I have two or three datagrams. How many times connect will signal that
    has datagram to be processed? ProcessPending digram will process one or all datagrams arrived? Or in this case I might read and read second datagram on the top of the first one?
    That might not be what I wanted?



  • @Tamis2018
    Not sure what you mean/what the problem is! :)

    This code taken from the net

    Then why not tell us the link?? Or do you enjoy setting us puzzles? :)

    Basically while (udpSocket.hasPendingDatagrams()) looks a bit odd.

    Why?!

    Let say that I have two or three datagrams. How many times connect will signal that
    has datagram to be processed?

    The signal is sent on readyRead().

    ProcessPending digram will process one or all datagrams arrived?

    processPendingDatagrams() has a loop. It will process as many datagrams as it finds are available, then it will exit.

    Or in this case I might read and read second datagram on the top of the first one?
    That might not be what I wanted?

    The code is just showing you how to read each datagram into a QByteArray. Yes, each one will overwrite in that buffer. I suspect the "code taken from the net" you mention intended you to do your processing of that data in the loop, after you have read one datagram into the buffer before you go read the next one on top of it!



  • @Tamis2018 have you tried running the whole "code taken from the net"? Does it work?

    You may want to check QUdpSocket documentation, which shows a quite similar way to read arriving datagrams.

    Essentially, you act on readyRead() signal "emitted whenever datagrams arrive" and loop until there are no more datagrams to read...



  • @JonB you might be right.
    http://www.informit.com/articles/article.aspx?p=1405552&seqNum=4
    in fact I could see that if there is pending datagram, read it, and then again, and as well process after each if statement. Ok. I understand it now.


Log in to reply