Results 1 to 4 of 4

Thread: QTcpSocket Misunderstandings

  1. #1
    Join Date
    Aug 2007
    Posts
    275
    Thanks
    28
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QTcpSocket Misunderstandings

    Hi all,

    I Use QTcpSocket to sendrequest to a device and in turn that device will answer, and I should not send another request until that device has all its reply sent.

    Request are small messages like max of 20 bytes but reply can vary from 10 000 to 30 000 bytes.

    Reply is stuctured by blocks and a reply may contain several blocks


    so I connected QTcpSocket::readyRead() signal to my readData slot (see below for readData slot implementation

    Qt Code:
    1. void Myclass::readData ()
    2. {
    3. QByteArray incomingBlock;
    4. while (m_deviceLink->socketMscope->bytesAvailable()) // I am suppose to suck all the reply with this
    5. {
    6. QByteArray packet = m_tcpSocket-> readAll();
    7. // append new data to block buffer
    8. incomingBlock.append(packet);
    9.  
    10. // check if its atleast a header
    11. if ( incomingBlock.count() > (int) sizeof(t_blockHeader))
    12. {
    13. m_response = *(t_blockHeader*)incomingBlock.constData();
    14. if (!isChecksumValid((uint8*)&m_response,sizeof(t_blockHeader))) // a header has checksum to verify its validity
    15. {
    16. emit errorCheckSum();
    17. incomingBlock.clear();
    18. break;
    19. }
    20. if (incomingBlock.count() == (int)(sizeof(t_blockHeader+m_response.dataLength))
    21. {
    22. // i got one block (header + data ) so I am suppose to proccess this then throw away the copy of the received socket data.
    23. m_incomingBlock.clear();
    24. }
    25. else if (m_incomingBlock.count() > (int)(sizeof(t_blockHeader)+ m_response.frameLength))
    26. {
    27. //I am expecting some more burst of data from the socket since some blocks are unfinished but processed the first complete block and trimmed incoming buffer
    28. m_incomingBlock = m_incomingBlock.mid(sizeof(t_blockHeader)+ m_response.dataLength);
    29. }
    30. else
    31. {
    32. // do nothing since received bytes is not even equal to one block
    33. }
    34. }
    35.  
    36. }
    37. emit readyForData(); //this will signal my requester class to send another request
    To copy to clipboard, switch view to plain text mode 

    So, I expected to have all my reply after the while loop but I was wrong my while exited halfway of the transmission of the reply

    Now, My Question is how can i suck all the large messages before exiting my while loop?

    I guess my payLoad of data is governed by tcpSocket buffer size, So I won't expect reply from the other side to be in one single shot of payLoad.

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTcpSocket Misunderstandings

    check the data for some ending condition...
    something like...

    Qt Code:
    1. do
    2. {
    3. data += socket->readAll();
    4. } while (data.contains(somecondition));
    To copy to clipboard, switch view to plain text mode 


    Hope u got the idea
    Last edited by jpn; 15th May 2008 at 08:50. Reason: changed [quote] to [code] tags

  3. The following user says thank you to aamer4yu for this useful post:

    baray98 (15th May 2008)

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

    Default Re: QTcpSocket Misunderstandings

    The fact that some data is available to read doesn't yet mean that all data is available, thus it is not enough to check if something is waiting to be read. You have to have a way to determine if you received a whole reply or not. You can base it on time but this is a flawed approach as lags can cause your timer to timeout before all of the data goes through the link or you can base it on the contents of the data received - like suggested in the previous post.

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

    baray98 (15th May 2008)

  6. #4
    Join Date
    Aug 2007
    Posts
    275
    Thanks
    28
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTcpSocket Misunderstandings

    I made it event and time driven and it seems to work.

Similar Threads

  1. QTcpServer & QTcpSocket questions...
    By jxmot in forum Qt Programming
    Replies: 2
    Last Post: 24th April 2008, 21:38
  2. QTcpSocket and libssh2 ...
    By sandros in forum Qt Programming
    Replies: 6
    Last Post: 21st November 2007, 22:34
  3. Using QTcpSocket
    By mraittin in forum Qt Programming
    Replies: 1
    Last Post: 2nd September 2007, 09:54
  4. Strange QTcpSocket behavior (debugged with Ethereal)
    By mdecandia in forum Qt Programming
    Replies: 23
    Last Post: 28th May 2007, 20:44
  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
  •  
Qt is a trademark of The Qt Company.