I am writing a simple multithreaded Http server using QTcpServer. The server spawns a new thread for every client connection. The new thread is being created in a slot connected to the readyRead() signal.


Qt Code:
  1. void serverwindow::AcceptClientConn()
  2. {
  3. ui->textEdit->append("\nNew client connection received");
  4.  
  5. clientConnection = tcpServer->nextPendingConnection();
  6.  
  7. ui->textEdit->append("\nNew client connection socketobtained");
  8.  
  9. connect(clientConnection, SIGNAL(disconnected()),
  10. clientConnection, SLOT(deleteLater()));
  11.  
  12. connect( clientConnection, SIGNAL(readyRead()),
  13. this, SLOT(readClient()) );
  14. }
  15.  
  16. void serverwindow::readClient()
  17. {
  18. //read the data obtained from client
  19.  
  20. ui->textEdit->append("\nreadClient");
  21. QTcpSocket* clientSocket = (QTcpSocket*)sender();
  22.  
  23. //create new thread to handle the client request
  24. clienthandler* clientThread = new clienthandler( clientSocket );
  25.  
  26. connect( clientThread,SIGNAL(finished()),
  27. clientThread,SLOT(deleteLater()) );
  28. clientThread->start();
  29. clientThread->setPriority(QThread::HighestPriority);
  30.  
  31. }
To copy to clipboard, switch view to plain text mode 


The clienthandle is a subclass of QThread. Its implementation of run() method is as below -

Qt Code:
  1. void clienthandler::run()
  2. {
  3. clientConnSocket->moveToThread(QThread::currentThread());
  4. if(clientConnSocket->canReadLine())
  5. {
  6. QString curData(clientConnSocket->readLine());
  7. QStringList tokens = curData.split(QRegExp("[ \r\n][ \r\n]*"));
  8. if ( tokens[0] == "GET" )
  9. {
  10. //try to send a file's contents
  11.  
  12. //1. Small sized html file
  13. QFile htmlfile("c:\\server_files\\index.html");
  14.  
  15. if (!htmlfile.open(QIODevice::ReadOnly))
  16. return;
  17.  
  18. QString content_type = "video/mp4;";
  19. QTextStream os( clientConnSocket );
  20. //os.setAutoDetectUnicode(true);
  21. os << "HTTP/1.0 200 Ok\r\n"
  22. "Content-Type: "<< content_type <<"charset=\"utf-8\"\r\n"
  23. "\r\n";
  24. os.flush();
  25.  
  26. // Streaming the file
  27. QByteArray block = htmlfile.readAll();
  28. clientConnSocket->write(block);
  29. }
  30. }
  31. clientConnSocket->disconnectFromHost();
  32. clientConnSocket->close();
  33. }
To copy to clipboard, switch view to plain text mode 



When i run the server, it receives client connections but issues the following error -

QObject::moveToThread: Cannot move objects with a parent
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QNativeSocketEngine(0x9640878), parent's thread is QThread(0x3d59e8), current thread is clienthandler(0x9644cb0)
QSocketNotifier: socket notifiers cannot be disabled from another thread



What changes should i make in the code to avoid these errors ? I know its the problem with the socket reference being passed to the new thread , but how else can I do it ? I know this question has been asked in this forum a lot many times, but I could not find a suitable answer.