Results 1 to 4 of 4

Thread: Socket Reading causes CPU to max out

  1. #1
    Join Date
    Jun 2008
    Location
    UK
    Posts
    35
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Socket Reading causes CPU to max out

    Hi,

    Ok, so im trying to asynchronously read data from a QTcpSocket, in a slot connected to its readyRead() signal.

    Every time the signal gets emitted, I readAll() and append it to a QByteArray buffer, after each readAll() call, I check to see if the buffer has the termination bytes in to know if I have all my data so I can stop.

    With 3mb of data coming down, it works fine but CPU usage is way to high and im after a solution if possible.

    Qt Code:
    1. void nclient::readSocket() // connected to QTcpSocket::readyRead()
    2. {
    3. buffer.append(client->readAll()); // put available bytes into buffer
    4. ...
    5. // buffer testing logic to find if all data has been received
    6. }
    To copy to clipboard, switch view to plain text mode 

    Its defiantly the readAll() calls that are maxing out the CPU and not my checking code, I can only assume its down to the large volumes of readyRead() signals, I think its 1 per packet and there's obviously 1000s of packets for a 3mb transmission...

    I don't know the size of the data in advance, so I cant enforce a minimum bytesAvailable size before reading data, all I know is how the stream ends.

    I would be really grateful if anyone can point me in the right direction, basically so I can download the stream without resource hogging and stop when I find the ending bytes.

    Thanks,

    Jack

  2. #2
    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: Socket Reading causes CPU to max out

    I'd rather think this is append() that is eating all your cpu power, not readAll().
    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.


  3. #3
    Join Date
    Jun 2008
    Location
    UK
    Posts
    35
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Socket Reading causes CPU to max out

    Hmm thanks alot, assuming the append() call is what's sucking up resources, could you recommend a more efficient approach to keeping a buffer maintained in memory?

  4. #4
    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: Socket Reading causes CPU to max out

    Why assume when you can check it out? Surround the calls with QTime::elapsed and you'll be certain.

    If you append to a large buffer, the buffer has to be reallocated and the data has to be copied to a new buffer which takes time. Either allocate some space in advance or store the data in several buffers.
    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.


Similar Threads

  1. Reading data from socket
    By ComaWhite in forum Qt Programming
    Replies: 1
    Last Post: 4th December 2008, 18:22
  2. How to Compile VTKDesigner2 with Qt?
    By alfredoaal in forum Newbie
    Replies: 0
    Last Post: 5th September 2008, 05:34
  3. Socket Reading Advice
    By tntcoda in forum Qt Programming
    Replies: 3
    Last Post: 4th July 2008, 11:26
  4. QWT 5, QT3, SuSE 10.2. Crash and burn
    By DrMcCleod in forum Qwt
    Replies: 8
    Last Post: 7th September 2007, 20:53
  5. Reading from TCP Socket crashes program
    By OnionRingOfDoom in forum Qt Programming
    Replies: 26
    Last Post: 27th January 2006, 19:32

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.