Results 1 to 4 of 4

Thread: QTcpSocket and data size - how do I know the size?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2007
    Location
    New York
    Posts
    45
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default QTcpSocket and data size - how do I know the size?

    I have a client/server application and I am confused about how the receiving side knows when enough data is available.

    The sender (which may be client or server) will send a single char to indicate mode, then a list of elements being requested or returned.

    for instance, if the client wants to request some variables from the server, it does something like:

    Qt Code:
    1. void class::sendData()
    2. {
    3. QByteArray block;
    4. QDataStream out(&block, QIODevice::WriteOnly);
    5. out.setVersion(QDataStream::Qt_4_0);
    6.  
    7. QChar c('v'); //indicate this is a variable request with 'v' char.
    8. QStringList varlist("var1") << "var2"; //indicate the list of vars being requested.
    9.  
    10. out << c;
    11. //out << dataSize //how do I calculate this value?
    12. out << varlist;
    13.  
    14. socket->write(block);
    15. }
    To copy to clipboard, switch view to plain text mode 

    how does the receiving side know when enough data is available? In the fortune examples, it writes the size of the string in the first bytes, which is fine for a single string.

    But how do I know the size of a QStringList? is it just the sum of the lengths of the strings, or is there a sizeof command which calculates it?

    After the request has been made, the recipient would respond with something like:

    Qt Code:
    1. void class::sendData()
    2. {
    3. QByteArray block;
    4. QDataStream out(&block, QIODevice::WriteOnly);
    5. out.setVersion(QDataStream::Qt_4_0);
    6.  
    7. QChar c('V'); //indicate this is a variable response with 'V' char.
    8. QHash<QString, QVariant> vars;
    9. vars["var1"] = QVariant(100);
    10. vars["var2"] = QVariant("some data here");
    11.  
    12. out << c;
    13. //out << dataSize //how do I calculate this value?
    14. out << vars;
    15.  
    16. socket->write(block);
    17. }
    To copy to clipboard, switch view to plain text mode 

    of course, the real implementation would fetch the values for the QHash from some other place based on the list previously received, and socket is a class member created from the connection.

    The documentation says that QHash is serializable, as well as QVariant, so it tells me that the << operator will do its job, but on the receiving side, how do I know how big the data is?

    Do I have to write it to a QByteArray, then measure the size of the bytearray, then write the size of the bytearray, then write the bytearray itself? This seems like an awful lot of copying and buffering that could be avoided if I knew the size (which will always be different) ahead of time. If I am sending a long list of variables, this could be expensive!

    I was considering creating a blocking TCP thread instead of returning when not enough bytes have arrived, but this has the same problem, because >> does not seem to block. Is there a way to make >> block, or a way to pre-calculate the size instead of putting it in a bytearray?

    thanks!
    Last edited by themolecule; 26th August 2007 at 23:10.

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.