Results 1 to 8 of 8

Thread: Parsing UDP datagram fields

  1. #1
    Join Date
    Oct 2010
    Posts
    11
    Thanks
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Parsing UDP datagram fields

    Hello,

    I am writing an application which has to receive UDP datagrams originated from an external source. I read the datagrams from the socket using QByteArray. The datagram has several fields, a 2-byte number, a 4-byte number and a textual message. I can read the textual message with the following code:

    Qt Code:
    1. QByteArray buffer;
    2. // ...
    3. // read datagram from socket
    4. // ...
    5. QString message = QString::fromAscii(buffer.mid(6, 10));
    To copy to clipboard, switch view to plain text mode 

    But the parsing of the other fields of the datagram doesn't work. For example:

    Qt Code:
    1. bool correct = true;
    2. quint16 cmd = buffer.mid(0, 2).toUShort(&correct);
    3. // correct is false after conversion
    To copy to clipboard, switch view to plain text mode 

    I assume I can't use QDataStream to get the individual fields, as the data is not being serialized to a QDataStream on the other end of the comunication. Could anyone point out what I am doing wrong? Is there maybe a better way to parse the different fields of a datagram in order to process them on the receiving end?

    Any help would be much appreciated.

    Thank you,
    catto

  2. #2
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Parsing UDP datagram fields

    Hi,

    I guess you want the 2 binary values at location 0 and 1 of the buffer to be interpreted as a 16-bit number ? Then just do something like nValue = (unsigned int)buffer[0] * 256 + (unsigned int)buffer[1]). Mind endianess though (which byte is MSB and which is LSB).

    The toUShort function is for converting something written as text in the buffer to an integer. See the documentation for QString::toULong() for an example.

    Best regards,
    Marc

  3. #3
    Join Date
    Oct 2010
    Posts
    11
    Thanks
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Parsing UDP datagram fields

    Thanks marcvanriet, interpret binary values of the buffer as numbers is exactly what I need, as I have to compare them with hexadecimal constants. What troubles me is exactly what you mention about minding endianess. Isn't there a way to parse the datagram fields in a platform-independant way?

  4. #4
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Parsing UDP datagram fields

    Hi,

    Endianess is defined in the IP and UDP protocol, so you can look this up (don't know it by heart). I usually just write my own getInt() or something method. It's just a few lines long. Its no use to make something platform independent (so targeted to the processor you use) if the protocol defines something else.

    Best regards,
    Marc

  5. #5
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Parsing UDP datagram fields

    you can use QDataStream.
    Warning: As default QDataStream uses BigEndian
    Last edited by mcosta; 18th February 2011 at 17:36.
    A camel can go 14 days without drink,
    I can't!!!

  6. #6
    Join Date
    Oct 2010
    Posts
    11
    Thanks
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Parsing UDP datagram fields

    Thank you marcvanriet, mcosta.

    mcosta, can you use QDataStream even if the other end of the communication is not using QDataStream to serialize data (it isn't even a Qt application) ?

  7. #7
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Parsing UDP datagram fields

    Yes, if you use only C/C++ base types such as char, int, ecc...
    A camel can go 14 days without drink,
    I can't!!!

  8. #8
    Join Date
    Oct 2010
    Posts
    11
    Thanks
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Parsing UDP datagram fields

    Thank you very much.

Similar Threads

  1. Reading last datagram in UDP network
    By euch in forum Qt Programming
    Replies: 5
    Last Post: 5th December 2010, 23:21
  2. Reading TCP datagram header by Qt
    By Kill3rReaper in forum Qt Programming
    Replies: 11
    Last Post: 8th June 2010, 12:49
  3. QUdpSocket - how to get destination address of a datagram?
    By nelliekins in forum Qt Programming
    Replies: 0
    Last Post: 17th September 2009, 08:59
  4. UDP datagram receive
    By mdannenb in forum Qt Programming
    Replies: 8
    Last Post: 27th July 2008, 03:30
  5. tr() and static fields
    By drkbkr in forum Qt Programming
    Replies: 6
    Last Post: 5th June 2007, 16:01

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.