Results 1 to 2 of 2

Thread: TCP: Don't process until a whole packet is received

  1. #1
    Join Date
    Jun 2011
    Posts
    38
    Thanks
    8
    Thanked 1 Time in 1 Post

    Default TCP: Don't process until a whole packet is received

    Hi everyone,

    before switchting to qt I used SFML for networking and there you have an sf::Packet class, in which you can insert data with the << operator and then you can send.
    The good thing with that is that it automaticully makes sure that the whole packet will be received is received and not part by part.

    I want to have the same behaviour with qt.

    I thought about sending the packet size first and checking in readyRead if the amount of bytes availble is >= the packet size.

    The client should not start to process the data until the whole packet is received.

    The problem with that is that before actually writing data to the QDataStream I need to figure out, how big the whole packet will be in order to be able to send this information before sending anything else.
    It would look something like this:
    Qt Code:
    1. int packetSize = 0;
    2. for(int i = 0; i < someVector.size(); ++i)
    3. {
    4. packetSize += someVector[i].getSize();
    5. }
    6. QDataStream stream(&socket);
    7. stream << packetSize;
    8. for(int i = 0; i < someVector.size(); ++i)
    9. {
    10. stream << someVector[i];
    11. }
    To copy to clipboard, switch view to plain text mode 

    I would prefer a solution where the size is automaticully figured out and written before anything else.

    With sfml it works like this:
    Qt Code:
    1. sf:.Packet toSend;
    2. for(int i = 0; i < someVector.size(); ++i)
    3. {
    4. toSend << someVector[i];
    5. }
    6. socket.Send(toSend);
    To copy to clipboard, switch view to plain text mode 
    Then on the client side
    Qt Code:
    1. sf::Packet toReceive;
    2. if(socket.Receive(toReceive) != sf::Socket::Done)
    3. {
    4. //not all data received yet, try again next time
    5. }
    6. else
    7. {
    8. //whole packet is there now, go and process it.
    9. }
    To copy to clipboard, switch view to plain text mode 
    The packet automaticully measures the size of its contents and sends it before sending the actual data so that the client knows how to figure out when the whole packet is received.

    Is there maybe some solution for qt to achieve this or something similar?

    Thanks for help in advance!

  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: TCP: Don't process until a whole packet is received

    The simplest self-contained thing that comes to my mind:

    Qt Code:
    1. class DataBlock {
    2. public:
    3. DataBlock() {
    4. clear();
    5. }
    6. void clear() {
    7. m_buf.close();
    8. m_buf.open(QIODevice::WriteOnly);
    9. m_stream.setDevice(&m_buf);
    10. }
    11. void writeTo(QIODevice *dev) {
    12. QByteArray dat = m_buf.data();
    13. quint32 s = dat.size();
    14. quint32 sNetwork = qToBigEndian(s);
    15. QByteArray sizeBlob = QByteArray((const char*)&sNetwork, 4);
    16. dev->write(sizeBlob);
    17. dev->write(dat);
    18. }
    19. bool readFrom(QIODevice *dev) {
    20. m_stream.setDevice(0);
    21. if(dev->bytesAvailable()<4) return false;
    22. QByteArray sizeBlob = dev->peek(4);
    23. quint32 size = qFromBigEndian(*((quint32)sizeBlob.constData()));
    24. if(dev->bytesAvailable()<4+size) return false;
    25. QByteArray data = dev->read(size);
    26. m_buf.setData(data);
    27. m_buf.open(QIODevice::ReadOnly);
    28. m_stream.setDevice(&m_buf);
    29. }
    30. QDataStream& stream() { return m_stream; }
    31. private:
    32. QDataStream m_stream;
    33. QBuffer m_buf;
    34. };
    To copy to clipboard, switch view to plain text mode 

    Disclaimer: not tested!
    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. Splitting QString
    By Peeri in forum Newbie
    Replies: 1
    Last Post: 13th April 2011, 13:41
  2. Reading all UDP packets
    By moron in forum Qt Programming
    Replies: 3
    Last Post: 27th May 2008, 15:51
  3. Updation of GUI truncates Packets
    By arunvv in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 11th July 2007, 19:13
  4. Splitting data so it fits into datagrams
    By toratora in forum Qt Programming
    Replies: 4
    Last Post: 27th April 2007, 18:01
  5. Splitting Translation Files
    By Jimmy2775 in forum Qt Programming
    Replies: 9
    Last Post: 3rd February 2006, 19:23

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.