Results 1 to 12 of 12

Thread: Reading TCP datagram header by Qt

  1. #1
    Join Date
    Jul 2009
    Location
    Poland, Łódź/Bełchatów
    Posts
    16
    Thanks
    3
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Question Reading TCP datagram header by Qt

    Hey!!
    Is there way to read header of TCP or UDP datagram by Qt?
    I can read only the data, which is sending to me (without header).

    Sorry for my english and thanks in advance for help!

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Reading TCP datagram header by Qt

    Not directly.
    Take a look at the code of QTcpSocket or QUdpSocket, maybe it will give you more information.
    You might even want to subclass those and expose some public functions to get the headers (if possible of course, check the source first)

  3. #3
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Reading TCP datagram header by Qt

    Scratch the post above.
    Maybe take a look at QNativeSocketEngine, it might give you what you want.
    It's not documented though.

  4. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Reading TCP datagram header by Qt

    Applications typically don't see the header. Thats handled by the OS.

    However, you can use libpcap to get such information portably.

  5. #5
    Join Date
    Jul 2009
    Location
    Poland, Łódź/Bełchatów
    Posts
    16
    Thanks
    3
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Reading TCP datagram header by Qt

    fatjuicymole, I think you're right. I'm trying to write a XMPP client (Jabber), and I don't know when packet is fragmented, and when it is whole. Size of packet dependents of MTU. I can't weather with it...

  6. #6
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Reading TCP datagram header by Qt

    For what you want to do, you don't need to go to the TCP level.
    You use the TCP connection via a QTcpSocket

    See this document:
    http://www.ietf.org/rfc/rfc3920.txt

  7. #7
    Join Date
    Jul 2009
    Location
    Poland, Łódź/Bełchatów
    Posts
    16
    Thanks
    3
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Reading TCP datagram header by Qt

    Exactly that I doing. I'm using QTcpSocket.
    For example: someone sending to me message (4000 chars = 4000 bytes). He sending it in one message, but I receive 3 packets (size of one packet depends for MTU; for example MTU = 1500 - most popular). I have got:
    1: 1500 bytes
    2: 1500 bytes
    3: 1000 bytes
    The size of each one is very often changeable, and in different places (routers for example) MTU can be different. I never know how large is packet and when message is complete.
    I hope, you're understanding what I mean.

  8. #8
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Reading TCP datagram header by Qt

    Quote Originally Posted by Kill3rReaper View Post
    (4000 chars = 4000 bytes).
    Not always true! Check encodings!

    He sending it in one message, but I receive 3 packets (size of one packet depends for MTU; for example MTU = 1500 - most popular). I have got:
    1: 1500 bytes
    2: 1500 bytes
    3: 1000 bytes
    The size of each one is very often changeable, and in very places (routers for example) MTU can be different. I never know how large is packet and when message is complete.
    I hope, you're understanding what I mean.
    I do, and no, you're not going to find TCP network infrastructure that sends you a gigabyte in one single packet. That would be silly.
    So, what to do...

    You need to receive a single file or message but it comes in several packets.
    A good protocol tells you when the data ends. In HTML for example, the size of the body is mentioned in the headers. In NNTP, a multiline message ends with \r\n.\r\n.
    So, find in the protocol you want to use how the server tells the clients that a specific request has ended.

  9. #9
    Join Date
    Jul 2009
    Location
    Poland, Łódź/Bełchatów
    Posts
    16
    Thanks
    3
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Reading TCP datagram header by Qt

    Quote Originally Posted by tbscope View Post
    Not always true! Check encodings!
    I know that - it is wrote in simplification

    A protocol of XMPP/Jabber looks like that:
    Qt Code:
    1. <message from="someone@jabber.org" to="someone2@jabber.org">
    2. <body>
    3. Content - message from user to user.
    4. </body>
    5. </message>
    To copy to clipboard, switch view to plain text mode 

    It looks OK, but what when user writes some of XML code...
    Qt Code:
    1. <message from="someone@jabber.org" to="someone2@jabber.org">
    2. <body>
    3. Content - message from user to user. [and a lot of strings, and now message have got 1500 bytes]
    4. </body> <--- user sends text, and he wrote to his friend a XML code like that
    5. </message>
    6. </body>
    7. </message>
    To copy to clipboard, switch view to plain text mode 

    Understand what I mean?
    Last edited by Kill3rReaper; 3rd June 2010 at 18:34.

  10. #10
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Reading TCP datagram header by Qt

    In this case, it is very simple.

    When reading from the socket, keep collecting the data. Everytime you receive data, check for "</message>" as that is the end of a message.
    When you receive "</message>" you can start again waiting for and reading data.

    In pseudocode:

    Qt Code:
    1. slotReadyRead()
    2. {
    3. data += readAll();
    4.  
    5. if (data.endsWidht("</message>")
    6. {
    7. doSomethingUsefullWith(data);
    8. data = "";
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

  11. The following user says thank you to tbscope for this useful post:

    Kill3rReaper (8th June 2010)

  12. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Reading TCP datagram header by Qt

    Quote Originally Posted by Kill3rReaper View Post
    fatjuicymole, I think you're right. I'm trying to write a XMPP client (Jabber), and I don't know when packet is fragmented, and when it is whole. Size of packet dependents of MTU. I can't weather with it...
    Fragmentation has nothing to do with the situation you describe. Fragmentation is a situation where a data segment (or packet, I don't remember whether fragmentation is done on transport or network layer) is transfered from a network segment with a larger MTU to a segment with a smaller MTU and the payload needs to be cut into pieces to fit into frames (and can later be reassembled when the frame size increases). What you are fighting against is data window size which is indeed dependant on the implementation of TCP (and its extensions) and data congestion in each of the network segments the data is travelling through. But still this has nothing to do with "messages" sent by the transmitter because TCP doesn't operate in terms of messages or datagrams. It is a constant flow (stream) of bytes. Its speed (flow) is regulated using buffers on both ends of transmission and window size that changes based on congestion. "Messages" are interpreted on the application level and that's all you have to deal with. With XMPP it is very easy as it is an xml based protocol - just use QXmlStreamReader and feed it with data as it comes in.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. The following user says thank you to wysota for this useful post:

    Kill3rReaper (8th June 2010)

  14. #12
    Join Date
    Jul 2009
    Location
    Poland, Łódź/Bełchatów
    Posts
    16
    Thanks
    3
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Reading TCP datagram header by Qt

    Hey!
    I did it at last
    I didn't know that, a XMPP message contains HTML entities, so solution by tbscope works.
    Thank you all!!

Similar Threads

  1. QStandardItem's header item and header label
    By feverzsj in forum Newbie
    Replies: 1
    Last Post: 14th January 2010, 19:57
  2. QUdpSocket - how to get destination address of a datagram?
    By nelliekins in forum Qt Programming
    Replies: 0
    Last Post: 17th September 2009, 08:59
  3. How to customize horizontal header (diagonal header view)
    By vairamuthu.g in forum Qt Programming
    Replies: 4
    Last Post: 4th September 2008, 15:59
  4. UDP datagram receive
    By mdannenb in forum Qt Programming
    Replies: 8
    Last Post: 27th July 2008, 03:30
  5. Replies: 2
    Last Post: 8th September 2006, 11:35

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.