title: Qt Multi-threaded Multi-client TCP server socket->write() issue
body: <p>I have a Qt GUI/console application for my TCP server. The server has an array of threads each containing a connection to a different client.</p>
  
  <p>If you have done multi-threaded TCP servers before then you would probably have seen a client thread with a run function something like.</p>
  
  <pre><code>    void ClientThread::run()
  {
  
      //thread starts here
      socket = new QTcpSocket();
      if(!socket-&gt;setSocketDescriptor(this-&gt;socketDescriptor))
      {
          emit error(socket-&gt;error());
          return;
      }
  
      connect(socket,SIGNAL(readyRead()),this,SLOT(readyRead()),Qt::DirectConnection);
      connect(socket,SIGNAL(disconnected()),this,SLOT(disconnected()),Qt::DirectConnection);
      connect(socket,SIGNAL(readyRead()),this,SLOT(readyRead()),Qt::DirectConnection);
  
      qDebug() &lt;&lt; socketDescriptor &lt;&lt; " Client Connected";
      emit threadDataOut(QString::number(socketDescriptor) + " Client Connected");
  
      exec();
  }
  </code></pre>
  
  <p>And with a readRead() function a ClientThread SLOT that receives from a readyRead() QTcpSocket SIGNAL I assume.</p>
  
  <pre><code>void ClientThread::readyRead()
  {
      QByteArray Data = socket-&gt;readAll();
  
       qDebug() &lt;&lt; socketDescriptor &lt;&lt; "Recived: " &lt;&lt; Data;
  
       //send data on to be displayed on GUI
       emit threadDataOut(Data);
  
       //Echo data back to client for testing purposes
       socket-&gt;write(Data);
  }
  </code></pre>
  
  <p>The data is received and is echoed back no problems. </p>
  
  <p>However I want to only send data that is passed from the user interface to the server to the client thread to a ClientThread::transmit(QByteArray Data) SLOT function.</p>
  
  <pre><code>void ClientThread::transmit(QByteArray Data)
  {
      qDebug()&lt;&lt;"Sent: "&lt;&lt; Data &lt;&lt;" to "&lt;&lt; this-&gt;socketDescriptor;
  
      socket-&gt;write(Data);
  }
  </code></pre>
  
  <p>So the correct client receives the data/message, but the server gives the error</p>
  
  <p>QObject: Cannot create children for a parent that is in a different thread.
  (Parent is QNativeSocketEngine(0x1b83e520), parent's thread is ClientThread(0x1b840da0), current thread is QThread(0x983390)</p>
  
  <p>From this point on the server is unable to receive any data sent by that particular client. For some reason the connection continues to work in the other direction.</p>
  
  <p>However if another data message is sent to that client the client receives it twice and if another data message is sent a third time the client receives message 3 times etc.</p>
  
  <p>Can anyone explain what may be going on here? and More importantly what would be an appropriate solution?</p>
  
  <p>Note: I have been testing on a windows computer using telnet clients on the local loop back 127.0.0.1
  I've refrained posting all my code for now, as I believe the problem is evident in what I have given thus far.</p>