title: How to make blocking tcp socket with Qt?
body: <p>I work with <code>QTcpSocket</code>. I need any write/read calls to the socket to be synchronous (blocking).</p>
  
  <p>I know there is <code>waitForReadyRead()</code> and <code>waitForBytesWritten()</code>, but those two methods are marked in Qt documentation as they can fail randomly under Windows. I cannot affort this.</p>
  
  <p>The blocking read is the most important (as reading comes always after writting a command to the other peer, so I know that if data reaches the other peer, it will answer).</p>
  
  <p>I have tried 2 approaches.</p>
  
  <p><strong>First:</strong></p>
  
  <pre><code>QByteArray readBytes(qint64 count)
  {
      int sleepIterations = 0;
      QByteArray resultBytes;
      while (resultBytes.size() &lt; count &amp;&amp; sleepIterations &lt; 100)
      {
          if (socket-&gt;bytesAvailable() == 0)
          {
              sleepIterations++;
              QThread::msleep(100);
              QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
              continue;
          }
  
          resultBytes += socket-&gt;read(qMin(count, socket-&gt;bytesAvailable()));
      }
      return resultBytes;
  }
  </code></pre>
  
  <p>This should wait for bytes to be available for reading on the socket, processing the event loop in the mean time, so the socket is doing it's necessary internal stuff.</p>
  
  <p>Unfortunately - for unknown to me reason - the <code>bytesAvailable()</code> sometimes returns correct number of bytes, but sometimes it never returns anything greater than 0.</p>
  
  <p>I know in fact that there was data to be read, because it used to work with the second approach (but it has it's own problems).</p>
  
  <p><strong>Second:</strong></p>
  
  <p>I have a kind of signal "blocker", which blocks current context and processes event loop, until certain signal is emitted. This is the "blocker":</p>
  
  <p>SignalWait.h:</p>
  
  <pre><code>class SignalWait : public QObject
  {
          Q_OBJECT
  
      public:
          SignalWait(QObject *object, const char *signal);
  
          bool wait(int msTimeout);
  
      private:
          bool called = false;
  
      private slots:
          void handleSignal();
  };
  </code></pre>
  
  <p>SignalWait.cpp:</p>
  
  <pre><code>SignalWait::SignalWait(QObject* object, const char* signal) :
      QObject()
  {
      connect(object, signal, this, SLOT(handleSignal()));
  }
  
  bool SignalWait::wait(int msTimeout)
  {
      QTime timer(0, 0, 0, msTimeout);
      timer.start();
      while (!called &amp;&amp; timer.elapsed() &lt; msTimeout)
          QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
  
      return called;
  }
  
  void SignalWait::handleSignal()
  {
      called = true;
  }
  </code></pre>
  
  <p>and then I used it like this:</p>
  
  <pre><code>SignalWait signalWait(socket, SIGNAL(readyRead()));
  // ...
  // socket-&gt;write(...);
  // ...
  if (!signalWait.wait(30000))
  {
      // error
      return;
  }
  bytes = socket-&gt;read(size);
  </code></pre>
  
  <p>This approach seems to be working better, but it also fails from time to time. I don't know why. It's like the <code>readyRead()</code> signal was never emitted and the <code>SignalWait</code> keeps waiting, until it times out.</p>
  
  <p>I'm out of ideas. What is the proper way to deal with it?</p>