title: QTcpSocket: reading and writing
body: <p>I know some similar questions may have been asked already, but the answers to those I found covered very specific problems and I still haven't figured it out.</p>
  
  <p>In my program I'm creating a QObject (called QPeer) that uses a QTcpSocket to communicate with another such object over a network. QPeer has a slot that accepts a QByteArray with data (<code>sendData(QByteArray)</code>). The entire contents of that array are considered to be one 'message' and they are written to the socket. I want to do the following: every time a message is written, I want the receiving QPeer to emit its signal <code>dataReceived(QByteArray)</code> exactly once, that QByteArray containing the entire message. (NOTE: all signals/slots, both private ones connecting the QPeer with its socket and the public ones such as <code>sendData(QByteArray)</code> are serialized by using <code>Qt::QueuedConnection</code> whenever necessary.)</p>
  
  <p>I use the signal <code>QTcpSocket::readyRead()</code> for asynchronous reading from the socket. Now I know I can't just call <code>QTcpSocket::write()</code> once in sendData and then assume that for every write I do, the QTcpSocket on the other side produces exactly one readyRead signal. So what should I do?</p>
  
  <p>This is my idea, please tell me if this will work:</p>
  
  <p>WRITING:</p>
  
  <pre><code>void QPeer::sendData(QByteArray data)
  {
      // TODO: write data.size() as raw int of exactly 4 bytes to socket
      const char *bytes = data.constData();
      int bytesWritten = 0;
      while (bytesWritten &lt; data.size())
          bytesWritten += _socket-&gt;write(bytes + bytesWritten);
  }
  </code></pre>
  
  <p>READING:</p>
  
  <p>now I want the read function (connected to <code>QTcpSocket::readyRead()</code>) to use the header (the 4 byte int specifying the length of the message) and then read that amount of bytes; next emit dataReceived with exactly those bytes. I'm having serious trouble trying to do this. <em>For example: what to do if readyRead is emitted and I can read the header of a message, but not the amount of bytes specified? Or what if a header has only been received partially?</em></p>
  
  <p><strong>1. How do I correctly write the header (4 byte int) to the socket?</strong></p>
  
  <p><strong>2. How do I correctly implement the read function so that it does what I want?</strong></p>
  
  <p>Any tips are welcome. Thanks!</p>