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...
-