Results 1 to 5 of 5

Thread: QTcpSocket speed problem

  1. #1
    Join Date
    Apr 2007
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Smile QTcpSocket speed problem

    Hi! I have a speed problem with QTcpSocket...

    I have to receive a series of variable-length packets in the classic tag-length-value format. That is, I have an header with the message type, its length and then the rest of the packet.

    I am trying to use asynchronous sockets to read and store packets. I am not used to this approach because I have always used blocking sockets and threads, but anyway I would like to learn. This is my first approach (pseudo-code):

    Qt Code:
    1. // This is the pseudo-code inside the readyRead() signal handling slot
    2. // packet is a struct build imitating the structure of my packet.
    3.  
    4. QDataStream in(tcpSocket)
    5. if (tcpSocket->bytesAvailable() < tag_size)
    6. return;
    7. in.readRawData((char*) &packet.tag, tag_size);
    8.  
    9. if (tcpSocket->bytesAvailable() < length_size)
    10. return;
    11. in.readRawData((char*) &packet.length, length_size);
    12.  
    13. if (tcpSocket->bytesAvailable() < packet.length)
    14. return;
    15. in.readRawData((char*) &packet.optionalPart, packet.length);
    To copy to clipboard, switch view to plain text mode 

    This is super-simplified... actually if the routines returns says at the second step ( tag read but length not available), the data already read from the buffer are kept and subsequent reads put the new data in queue to the buffer.

    Anyway, this works fine but its slow! No packets are lost but the reception speed is so slow that the receiving buffer grows to hundreds of megabytes in a minute or two. Consider that I am trying to receive (in gross) something around 5000 packets per second, where each packet is in average 256 byte. What I get instead is around 100 packet per seconds.

    Is that too much to handle with async socket?

    Sorry for the pseudo-code but I am a little bit in a rush. If this topic is interesting for someone, I'll post some more details.

    Thanks to anyone that is willing to give me some hints.

    Bye!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTcpSocket speed problem

    What happens to the buffer if you simply discard the data after reading it?

  3. #3
    Join Date
    Apr 2007
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTcpSocket speed problem

    Good question. I'll try it as soon as I get back to my workplace... I cannot access the server from here unfortunately.

    After some more googling, I found some posts where people say that while handling readyRead(), after calling bytesAvailable(), it would be wise to read ALL the available data on the socket in one time. Does someone agree with that? Anyway, it sounds awkward to me: it would be like not having a tcp buffer at all.

    Again, I'll try it in the next days. Ciao

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTcpSocket speed problem

    Yes I agree, at least in theory. The socket buffer most likely is much smaller than 5000*256 bytes.

    You could read it all( or less ) in a buffer and then parse it.

    regards

  5. #5
    Join Date
    Jan 2006
    Location
    Norway
    Posts
    124
    Thanked 38 Times in 30 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTcpSocket speed problem

    Oasn, very often performance problems in QTcpSocket are caused by one of the following

    • Not reading (QTcpSocket will reallocate to grow its internal buffer)
    • Reading with the QByteArray functions (a new bytearray is allocated for each packet)
    • Running network code in the same thread as intensive GUI code (a progress bar will prevent the socket from being read from while continuously drawing itself, which again causes the socket buffer to fill up and start rejecting packets)
    Bitto / Andreas Aardal Hanssen - andreas dot aardal dot hanssen at nokia
    Nokia Software Manager, Qt Development

Similar Threads

  1. Can't create an object : initialisation problem ?
    By Nyphel in forum Qt Programming
    Replies: 5
    Last Post: 12th March 2007, 09:07
  2. problem with QTcpSocket
    By SuperSonik in forum Qt Programming
    Replies: 8
    Last Post: 31st January 2007, 16:00
  3. Problem with QTcpSocket and QDataStream
    By Valheru in forum Qt Programming
    Replies: 4
    Last Post: 16th September 2006, 13:08
  4. Replies: 16
    Last Post: 7th March 2006, 15:57
  5. QTcpSocket disconnection problem
    By erdi in forum Qt Programming
    Replies: 4
    Last Post: 19th February 2006, 21:50

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.