try a method/class like (untested)
Qt Code:
  1. class InputProcessor : public QObject
  2. {
  3. Q_OBJECT
  4.  
  5. QIODevice *input_;
  6. qint64 blocksize_;
  7. QDataStream datastream_;
  8.  
  9. public:
  10. InputProcessor(QIODevice *input, QObject *parent=0)
  11. : QObject(parent)
  12. , blocksize_(0)
  13. , input_(input)
  14. , datastream(input)
  15. {
  16. connect(input_, SIGNAL(readyRead()), SLOT(slotProcessInput()));
  17. }
  18.  
  19. public slots:
  20. // connect tcp socket readyRead() here
  21. void slotProcessInput()
  22. {
  23. // if we do not have a block size, wait for enough bytes
  24. if (blocksize_==0)
  25. { // wait for blocksize
  26. if (input_->bytesAvailable() < sizeof(qint64))
  27. return;
  28. datastream_ >> blocksize_;
  29. }
  30.  
  31. // now we know the size of the transmitted block: wait for the data
  32. if (input_->bytesAvailable() < blocksize_)
  33. return; // we will be called again
  34.  
  35. // all data here, read block:
  36. // datastream_ >> ...;
  37.  
  38. blocksize_ = 0; // wait for next block (size)
  39. }
  40.  
  41. };
To copy to clipboard, switch view to plain text mode 

(I have not tried to compile that. Please post if it is buggy.)

HTH