Results 1 to 15 of 15

Thread: Packet getting fragmented when sending over QTcpSocket

  1. #1
    Join Date
    Mar 2009
    Location
    Belchatow, Poland
    Posts
    38
    Thanks
    11
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Packet getting fragmented when sending over QTcpSocket

    I'm trying to write a single class to support XMPP protocol, it's packets are XML nodes.
    I'm using QTcpSocket for connection with the server, but sometimes when I get a big packet the readReady signal is emited twice and I get the packet fragmented. What's the best way to bypass this? I know I can have a buffer that I write all incoming data to and process the nodes when they are done, but i this the best and most optimal way? I'm quite new to Qt so it's possible I'm missing something, I'm learning new things every day.

    Fragmentation with XMPP is not even half as confuing as fragmentation with a binary protocol. I'm also trying to write a class to support Gadu-Gadu (polish IM protocol) and it's packages are binary - there's always a "header" packet that tells me what type of packet I'm going to get next and what' it's size and then I get the packet, but sometimes it also get's fragmented. I haven't try to bypass this by using a buffer cause it's not easy finding out where one packet ends and another header starts.

    Any help appreciated.

  2. #2
    Join Date
    Mar 2009
    Location
    Belchatow, Poland
    Posts
    38
    Thanks
    11
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: Packet getting fragmented when sending over QTcpSocket

    anybody?

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

    Default Re: Packet getting fragmented when sending over QTcpSocket

    Quote Originally Posted by arturo182 View Post
    I'm using QTcpSocket for connection with the server, but sometimes when I get a big packet the readReady signal is emited twice and I get the packet fragmented. What's the best way to bypass this? I know I can have a buffer that I write all incoming data to and process the nodes when they are done, but i this the best and most optimal way? I'm quite new to Qt so it's possible I'm missing something, I'm learning new things every day.
    You can either buffer the data in your application or use QXmlStreamReader for parsing the xml. It can operate on incomplete data - when more data is ready, you can ask it to continue where it left off. There is also a third choice - do nothing (ignore readyRead()) until the whole package of data arrives. If the messages are small enough, they will fit into the internal buffer so you don't have to keep making room for more data.


    Fragmentation with XMPP is not even half as confuing as fragmentation with a binary protocol.
    It's not fragmentation, it's analog world where things don't happen in an instant.
    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.


  4. #4
    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: Packet getting fragmented when sending over QTcpSocket

    a third choice - do nothing (ignore readyRead()) until the whole package of data arrives. If the messages are small enough, they will fit into the internal buffer so you don't have to keep making room for more data.
    But this method will freez program, until rest of package will not receive to us, right?

    P.S: Sorry for my poor english...
    Last edited by Kill3rReaper; 25th July 2009 at 13:27.

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

    Default Re: Packet getting fragmented when sending over QTcpSocket

    Quote Originally Posted by Kill3rReaper View Post
    But this method will freez program, until rest of package will not receive to us, right?
    No, why? Socket operations are asynchronous, they don't freeze your gui.
    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.


  6. #6
    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: Packet getting fragmented when sending over QTcpSocket

    Look at this example:
    Qt Code:
    1. <message from="smn@jabber.com"><body>Lorem ipsum... (and a lot of text)</body></message>
    To copy to clipboard, switch view to plain text mode 
    If this verse will longer than, for example, 2000 bytes, then this message, will be broken in the, again for example :P, 1500 byte. Rest 500 bytes will be send in new packet. So, I don't know, where my message ends, because between tags <body> and </body> I can send XML code, which can have inside tag </body> or </message>
    I hope you understand my english. If no, can I write to you pm in polish language?

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

    Default Re: Packet getting fragmented when sending over QTcpSocket

    The fact that it comes in parts is meaningless and has nothing to do with freezing anything. If you know what you expect then you can wait until your expectations are filled. Doing this by ignoring the data is probably not the best idea but it is possible. A better way is to read the pending data (thus freeing the incoming buffer), scanning that data for end of message (which is easy in your case) and either processing the data or waiting until more comes in. Remember that you can also get more than one message in a single go, you need to handle situations like that as well.
    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.


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

    SIFE (10th June 2011)

  9. #8
    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: Packet getting fragmented when sending over QTcpSocket

    Ehhhh, I thought there was some other way to do it
    Anyway, thank you for answer!
    Czesc
    Last edited by Kill3rReaper; 25th July 2009 at 22:24.

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

    Default Re: Packet getting fragmented when sending over QTcpSocket

    Quote Originally Posted by Kill3rReaper View Post
    Ehhhh, I thought there was some other way to do it
    Sure there is. Use QXmlStreamReader.
    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.


  11. #10
    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: Packet getting fragmented when sending over QTcpSocket

    Ehhh... I just was writing new class for XML packets, when I try to write something XML to <body> </body> section... Code between these tags is interpretting by my algorism... When I was using QDomElement, I did't have got that problem What I can do?

  12. #11
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Packet getting fragmented when sending over QTcpSocket

    and what is the problem exactly?
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  13. #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: Packet getting fragmented when sending over QTcpSocket

    My english sux very much...
    For example:
    Qt Code:
    1. <message from='test@jab.com/Jabber' type='chat' xml:lang='pl' to='test@jabbim.pl'><active xmlns='http://jabber.org/protocol/chatstates'/><body>Hey!</body></message>
    To copy to clipboard, switch view to plain text mode 
    If I change body section to: "Hej <someth" my application freezes.

  14. #13
    Join Date
    Feb 2009
    Location
    Lexington, KY
    Posts
    8
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Packet getting fragmented when sending over QTcpSocket

    Quote Originally Posted by Kill3rReaper View Post
    My english sux very much...
    If I change body section to: "Hej <someth" my application freezes.
    I have not used the XML parser in Qt before, but other XML parsers would interpret the '<' symbol in the body as the opening to a tag. The parser would then read until it finds a closing '<' tag. Try converting the '<' to "&lt;" and see if your program still hangs.

    You can do the string conversion manually or for '<' and '>' with QString's replace string functions. There might be a method in one of the XML classes to quote characters that are meaningful to XML for you.

  15. #14
    Join Date
    Mar 2009
    Location
    Belchatow, Poland
    Posts
    38
    Thanks
    11
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: Packet getting fragmented when sending over QTcpSocket

    Well, he can't change the packet cause that's the way the server sends it.

    My solution would be if you see the opening "body" tag, treat everything that follows as text untill you reach closing "body" tag. But that's not perfect cause someone could be chatting about HTML and send </body> and that would screw the parser up.

  16. #15
    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: Packet getting fragmented when sending over QTcpSocket

    Quote Originally Posted by wrdieter View Post
    I have not used the XML parser in Qt before, but other XML parsers would interpret the '<' symbol in the body as the opening to a tag. The parser would then read until it finds a closing '<' tag. Try converting the '<' to "&lt;" and see if your program still hangs.

    You can do the string conversion manually or for '<' and '>' with QString's replace string functions. There might be a method in one of the XML classes to quote characters that are meaningful to XML for you.
    It's good idea
    I thought about use QDomElement class to solve this problem, but your solution sounds good
    P.S: I know that the XML parser interprets '<' symbol as the opening tag, but I could not cope with this ;/
    Last edited by Kill3rReaper; 5th August 2009 at 23:59.

Similar Threads

  1. QUdpSocket error
    By mdecandia in forum Qt Programming
    Replies: 8
    Last Post: 25th October 2007, 10:47
  2. qt network performance
    By criss in forum Qt Programming
    Replies: 16
    Last Post: 24th July 2006, 09:23

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.