Results 1 to 12 of 12

Thread: Reading TCP datagram header by Qt

Hybrid View

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

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 268 Times in 268 Posts
    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.

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

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 268 Times in 268 Posts
    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 

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

    Kill3rReaper (8th June 2010)

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
  •  
Qt is a trademark of The Qt Company.