Results 1 to 5 of 5

Thread: QTcpSocket readyRead and buffer size

  1. #1
    Join Date
    Jan 2008
    Posts
    107
    Thanks
    36
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question QTcpSocket readyRead and buffer size

    Hi all,

    Does anyone know the size at which the buffer is filled when readyRead signal is emitted?

    Thanks in advance.
    Pedro Doria Meunier.

  2. #2
    Join Date
    Jan 2008
    Location
    Silicon valley
    Posts
    15
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTcpSocket readyRead and buffer size

    As soon as there is something in the buffer (even if it's a single byte). Look in bool QAbstractSocketPrivate::_q_canReadNotification() in src/network/qabstractsocket.cp

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

    pdoria (1st February 2008)

  4. #3
    Join Date
    Jan 2008
    Posts
    107
    Thanks
    36
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTcpSocket readyRead and buffer size

    Txs for your answer.

    I've got a situation here where I cannot determine whether it's the device tx scheme or the size of the socket read buffer than come into play...

    The thing is:
    The device reports n Bytes to be read (the physical thing).
    and the readyRead gets fired at 255 bytes.

    So when I try to parse the data I'm n bytes short.
    Dirty solution:
    In the method connected to the readyRead signal I compare the tcpSocket.bytesAvailable() to the # of bytes the device says it wants to transmit.
    In a nutshell:
    Qt Code:
    1. if (totalBytes < dataLength)
    2. {
    3. tcpSocket.waitForReadyRead(5000);
    4. }
    To copy to clipboard, switch view to plain text mode 

    Bottom line (and question ):
    Is there anyway to wait until n Bytes are available in the buffer instead of n milliseconds?

    Thanks in advance.
    Pedro Doria Meunier.

  5. #4
    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 readyRead and buffer size

    TCP is a stream based protocol... you will get data... but no fixed length.

    to read N bytes... u can connect the readyRead to a slot, and have a static buffer there. When the bytes read equals N bytes, u can call some member function to process the N bytes.

    Qt Code:
    1. MySocket::OnReadyRead()
    2. {
    3. static buffer[1024];
    4. buffer = readAll();
    5. if(buffer.lenght() >= m_N)
    6. {
    7. processNbytes(buffer);
    8. buffer.clear();
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

  6. #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 readyRead and buffer size

    It's 1000000 times better to just leave the data in the socket, and return. The next time this signal is fired, you'll probably have enough. So:

    Qt Code:
    1. void MyClass::slotConnectedToReadyRead()
    2. {
    3. // Bad:
    4. static QByteArray data; // static data is bad
    5. data += readAll(); // unnecessary copy
    6. if (data.size() < 256)
    7. return;
    8.  
    9. // Good:
    10. if (bytesAvailable() < 256)
    11. return;
    12. QByteArray data = read(256); // local variable is good
    13. }
    To copy to clipboard, switch view to plain text mode 
    Bitto / Andreas Aardal Hanssen - andreas dot aardal dot hanssen at nokia
    Nokia Software Manager, Qt Development

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.