title: read and write to qtcpsocket using qdatastream
body: <p>There are various methods of reading and writing from a <a href="http://doc.qt.io/qt-5/qtcpsocket.html" rel="nofollow noreferrer">QTcpSocket</a> using a <a href="http://doc.qt.io/qt-5/qdatastream.html" rel="nofollow noreferrer">QDatastream</a> as seen <a href="https://stackoverflow.com/questions/19681564/read-from-qtcpsocket-using-qdatastream">here</a></p>
  
  <p>The difference is, I will be sending more than "one packet" or blocks.</p>
  
  <p><em>A basic implementation on the server (sending) side and client (recieving) is seen below - only the actual sending and receiving snippets are shown</em></p>
  
  <p><strong>More Info, What I tried:</strong></p>
  
  <p>When writing to a QTcpSocket, I attempted to use the <code>QTcpSocket::canReadLine()</code> however this fails straightup after the <code>QTcpSocket::waitForReadReady()</code> signal fires.</p>
  
  <p>I then tried <code>QDataStream::atEnd()</code> in a while loop which causes a system crash :(</p>
  
  <p>The code below shows my latest attempt of going through the QDataStream docs, and utilzing the commitTransaction where it states </p>
  
  <blockquote>
    <p>If no full packet is received, this code restores the stream to the initial position, after which you need to wait for more data to arrive.</p>
  </blockquote>
  
  <p>Under the heading <code>Using Read Transactions</code>. But ofcourse, this just reads one block that is sent, i.e the first block.</p>
  
  <p><strong>Question:</strong></p>
  
  <p>When writing to a <code>QTcpSocket</code> multiple times, and flushing the socket each time to send that data, how can I read this from a <code>QTcpSocket</code> as it is send, keep the original "send structure"?</p>
  
  <p>The example below only reads the first block and ends. I would like to read the block containing "Response 2" and "Response 3".</p>
  
  <p><strong>Code Implementations:</strong></p>
  
  <p>//server.h</p>
  
  <pre><code>//...
  QTcpSocket *clientSocket = nullptr;
  QDataStream in;
  //...
  </code></pre>
  
  <p>//server.cpp </p>
  
  <pre><code>//...
  in.setDevice(clientSocket);
  in.setVersion(QDataStream::Qt_4_0);
  
  in.startTransaction();
  
  QString nextFortune;
  in &gt;&gt; nextFortune;
  
  if (in.commitTransaction())
      ui-&gt;lblOut-&gt;setText(nextFortune);
  
  if (clientSocket != nullptr) {
      if (!clientSocket-&gt;isValid()) {
          qDebug() &lt;&lt; "tcp socket invalid";
          return;
      }
      if (!clientSocket-&gt;isOpen()) {
          qDebug() &lt;&lt; "tcp socket not open";
          return;
      }
  
      QByteArray block;
      QDataStream out(&amp;block, QIODevice::WriteOnly);
      out.setVersion(QDataStream::Qt_4_0);
  
      out &lt;&lt; QString(QString("Response:") + nextFortune);
      if (!clientSocket-&gt;write(block)){
          QMessageBox::information(this, tr("Server"),tr("Could not send message"));
      }
      clientSocket-&gt;flush();
  //        block.clear();
  
      out &lt;&lt; QString("Response number 2");
      if (!clientSocket-&gt;write(block)){
          QMessageBox::information(this, tr("Server"),tr("Could not send message"));
      }
      clientSocket-&gt;flush();
  //        block.clear();
  
      out &lt;&lt; QString("Response number 3 here, and this is the end!");
      if (!clientSocket-&gt;write(block)){
          QMessageBox::information(this, tr("Server"),tr("Could not send message"));
      }
      clientSocket-&gt;flush();
      clientSocket-&gt;disconnectFromHost();
  
  }
  //...
  </code></pre>
  
  <p>And the client side</p>
  
  <p>//client.h</p>
  
  <pre><code>//...
  QTcpSocket *tcp_con = nullptr;
  QDataStream in;
  //...
  </code></pre>
  
  <p>//client.cpp</p>
  
  <pre><code>//...
  if(!tcp_con-&gt;waitForReadyRead()){
      qDebug(log_lib_netman_err) &lt;&lt; "tcp con timeout for reading";
      tcp_con-&gt;disconnectFromHost();
      return ReturnObject(ReturnCode::SocketError, QString());
  }
  
  in.setDevice(tcp_con);
  in.setVersion(QDataStream::Qt_4_0);
  
  in.startTransaction();
  
  QList&lt;QString&gt; data_rcv = QList&lt;QString&gt;();
  QString s;
  
  //    while (tcp_con-&gt;canReadLine()) {
  //        in &gt;&gt; s;
  //        data_rcv.push_back(s);
  //    }
  //    while (!in.read) {
  in &gt;&gt; s;
  data_rcv.push_back(s);
  //    }
  
  
  while (!in.commitTransaction()){
      qDebug(log_lib_netman_info) &lt;&lt; "waiting for more data";
      in &gt;&gt; s;
      data_rcv.push_back(s);
      //        qDebug(log_lib_netman_err) &lt;&lt; "Unable to send data to server";
      //        tcp_con-&gt;disconnectFromHost();
      //        return ReturnObject(ReturnCode::FailedReceiving, QString());
  }
  
  //    if (s.isEmpty()) {
  //        qDebug(log_lib_netman_err) &lt;&lt; "Empty response recieved";
  //        tcp_con-&gt;disconnectFromHost();
  //        return ReturnObject(ReturnCode::NoDataRecieved, QString());
  //    }
  
  tcp_con-&gt;disconnectFromHost();
  return ReturnObject(ReturnCode::ReceivedSuccess, data_rcv);
  </code></pre>
  
  <p>Help would be greatly appreciated!</p>