Results 1 to 11 of 11

Thread: QTcpSocket slowdown

  1. #1
    Join Date
    Jun 2008
    Posts
    14
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default QTcpSocket slowdown

    I implement an UI application, that handles receiving data from tcp socket in a separate thread.

    I use the following code to read data:
    Qt Code:
    1. data = m_Connection->readAll();
    2. m_CurrentResponse->insert(m_CurrentResponse->size(), data);
    3. while (isResponseFull(m_CurrentResponse))
    4. {
    5. QByteArray* response = new QByteArray(m_CurrentResponse->data(), m_CurrentResponse->size());
    6.  
    7. unsigned int len = // read the 4 bytes to get the length of the responce
    8. response->remove(len, (m_CurrentResponse->size() - len));
    9. m_CurrentResponse->remove(0, len);
    10. processResponse(response);
    11. delete(response);
    12. }
    To copy to clipboard, switch view to plain text mode 
    It crashes at readAll after few minutes. The server sends image data for every 1 ms. The client application should not block while processing data.

    1) Is there any issue in calling readAll when bytesAvailable is more in number?
    2) setReadBufferSize does not help in setting buffer size. Is there any way to restrict to limit receiving data internally in tcpSocket object
    Last edited by wysota; 3rd September 2009 at 08:17. Reason: missing [code] tags

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

    Default Re: QTcpSocket slowdown

    Where (which thread) did you create the socket? Where (which thread) do you read the data? Why do you allocate a byte array on heap?
    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
    Posts
    14
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTcpSocket slowdown

    1) I create tcp Socket in worker thread(run)
    2) I read data in slot of readyRead signal in worker thread.
    3) There is no specific reason to have qbytearray in heap, will it cause any issue?

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

    Default Re: QTcpSocket slowdown

    Quote Originally Posted by jessiemmichael View Post
    1) I create tcp Socket in worker thread(run)
    2) I read data in slot of readyRead signal in worker thread.
    Does something else access the socket as well? Does the thread do anything else apart handling the socket?

    3) There is no specific reason to have qbytearray in heap, will it cause any issue?
    It's just harder to handle the byte array because you have to remember to delete it without giving any benefits in exchange.
    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.


  5. #5
    Join Date
    Jun 2008
    Posts
    14
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTcpSocket slowdown

    1) Worker thread is responsible for reading data, it parses the data and queues up the data accordingly. The UI thread retrieves the data and displays it.
    The socket is accessed only by the worker thread.

    2) I have taken care of freeing the qbytearray.

    Is there any possibility that any byte of the data in QTcpSocket object get discarded internally .

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

    Default Re: QTcpSocket slowdown

    No, there is no such possibility. Try getting rid of the thread, you don't need it anyway as Qt sockets are asynchronous.
    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.


  7. #7
    Join Date
    Jun 2008
    Posts
    14
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTcpSocket slowdown

    Initially UI application was implemented without worker thread for communication.

    This way of implementation causes server to slowdown when UI is busy. To overcome this issue, I plan to have thread.

    Is there any other alternative solution?

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

    Default Re: QTcpSocket slowdown

    How many cores or processors do you have in your machine?

    Oh, one more thing - where is the slot connected to the readyRead() signal of the socket located?
    Last edited by wysota; 3rd September 2009 at 13:55.
    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.


  9. #9
    Join Date
    Jun 2008
    Posts
    14
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTcpSocket slowdown

    Core2 duo processer.

    Signal connection is in worker thread's run method.
    Slot is provided in worker thread class.

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

    Default Re: QTcpSocket slowdown

    Quote Originally Posted by jessiemmichael View Post
    Slot is provided in worker thread class.
    That's the reason for your slowdown. The slot is executed in the context of the main thread so your thread does practically nothing and the whole strain goes on the main thread. The slot has to be in an object created from within the run() method.

    I don't know what you mean by "processing data" but there is a good chance you can just optimize it and put in the same thread as the 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.


  11. #11
    Join Date
    Jun 2008
    Posts
    14
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTcpSocket slowdown

    Thank you very much. Having the slot in different class and creating in in the worker thread resolves this issue.

Similar Threads

  1. Challenging QTcpSocket reliability problem
    By Raccoon29 in forum Qt Programming
    Replies: 3
    Last Post: 13th January 2009, 10:38
  2. array of pointers to QtcpSocket
    By gQt in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 16th June 2008, 09:15
  3. QTcpServer & QTcpSocket questions...
    By jxmot in forum Qt Programming
    Replies: 2
    Last Post: 24th April 2008, 21:38
  4. QTcpSocket and libssh2 ...
    By sandros in forum Qt Programming
    Replies: 6
    Last Post: 21st November 2007, 22:34
  5. Strange QTcpSocket behavior (debugged with Ethereal)
    By mdecandia in forum Qt Programming
    Replies: 23
    Last Post: 28th May 2007, 20:44

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.