[Qt-interest] QUdpSocket issue
Arnold Krille
arnold at arnoldarts.de
Wed Jan 20 20:38:03 CET 2010
Hi,
On Wednesday 20 January 2010 18:51:55 Davide wrote:
> I’ve got a strange behavior using QUdpSocket class: my pc receives a
> set of UDP packets that I can list with Wireshark but some of them are
> not received by the QUdpSocket object in my application.
First: Note that udp is a connection-less transaction, it is very likely that
packets get lost and there is no way in the protocol itself to deal with that.
And these losses can happen anywhere in the osi-stack.
Also you should send small datagrams, bigger datagrams are more likely to get
eaten somewhere inside the stack.
And the order the packets are received is not necessarily the order in which
the packets are received...
<snip>
> How is it possible that my pc receives all the packets but for some
> reason these packets don’t reach my QUdpSocket?
> P.S.: Below there is the piece of code that wait for the whole image.
>
> while (1) {
> if(waitForReadyRead(1500)) {
> size = pendingDatagramSize();
> datagramData = (char*) malloc(size);
> if(readDatagram(datagramData) != 0) {
> switch(ParseMessage(datagramData, size, sync_Get_Image, pktCount)) {
> case EnetCmd_ST_IMG:
> case EnetCmd_N_IMG:
> pktCount++;
> break;
> case EnetCmd_END_IMG:
> free(datagramData);
> return pktCount; // OK received Ack
> }
> }
> free(datagramData);
> } else {
> return 0;
> }
> }
You only read one datagram upon readyRead(). But this signal is emitted only
once even when multiple datagrams have arrived. Inside the loop waiting for
waitForReadyRead() you have to loop until pendingDatagramSize() returns -1 to
read all datagrams currently in buffer. Plus you will only read from the buffer
when there are actual datagrams there. Your current code tries to create a
char* of length -1 when readyRead() is emitted while no datagram can be read.
Have fun,
Arnold
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100120/dbfc6cf5/attachment.bin
More information about the Qt-interest-old
mailing list