PDA

View Full Version : Reading last datagram in UDP network



euch
5th December 2010, 20:06
Hello,

I have a server which send datagram in very high rate. however I want to sample
the data every one second and i want to get the last datagram. I don't want to get any signals of readyread between my requests to sample the data and i want fast access to the last datagram. any solution?

high_flyer
5th December 2010, 20:17
any solution?
to what problem?
or should we guess?

franz
5th December 2010, 20:23
Here's a concept:

QByteArray dgram;
while (socket->hasPendingDatagrams()) {
// do some resize magic
socket->readDatagram(dgram.data()...);
}
// dgram == last datagram, or null.
if (!dgram.isNull())
parseDatagram(dgram);

There's a lot you can think of to fill in the details by just reading the documentation.

euch
5th December 2010, 20:36
I have a server which send realtime data in very fast rate (every 1 milisecond) but I need to read the data in low rate (every 1 second). The data is sent as datagram in UDP network. I need to read only the last datagram because all the previous datagrams are obsolete for me. if I read every datagram that I get I will also work in 1 milisecond. If I wait 1 second and then connect to readyRead signal i will have to iterate all the datagram untill I reach the last one (and then I will have to disconnect readyRead). I want to be able to read the last datagram every one second in simple way.

franz
5th December 2010, 20:47
This is probably as simple as you can get it. The QUdpSocket doesn't give you the option of discarding things in another way than reading the messages and discarding them yourself.

wysota
6th December 2010, 00:21
One thing to note - there is no concept of "last" datagram with UDP. UDP doesn't guarantee that the sending order of datagrams is preserved on the receiving end so it may happen that the last datagram you receive is the first one of the whole series that was sent. Or that you don't receive the datagram at all. A common misconception in network programming is to treat the sender and receiver as one linked system (ignoring the nature of protocols connecting the systems) instead of treating them as autonomous systems exchanging data according to a set of rules (the protocol).