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.
void serverwindow::AcceptClientConn()
{
ui->textEdit->append("\nNew client connection received");
clientConnection = tcpServer->nextPendingConnection();
ui->textEdit->append("\nNew client connection socketobtained");
connect(clientConnection, SIGNAL(disconnected()),
clientConnection, SLOT(deleteLater()));
connect( clientConnection, SIGNAL(readyRead()),
this, SLOT(readClient()) );
}
void serverwindow::readClient()
{
//read the data obtained from client
ui->textEdit->append("\nreadClient");
//create new thread to handle the client request
clienthandler* clientThread = new clienthandler( clientSocket );
connect( clientThread,SIGNAL(finished()),
clientThread,SLOT(deleteLater()) );
clientThread->start();
clientThread
->setPriority
(QThread::HighestPriority);
}
void serverwindow::AcceptClientConn()
{
ui->textEdit->append("\nNew client connection received");
clientConnection = tcpServer->nextPendingConnection();
ui->textEdit->append("\nNew client connection socketobtained");
connect(clientConnection, SIGNAL(disconnected()),
clientConnection, SLOT(deleteLater()));
connect( clientConnection, SIGNAL(readyRead()),
this, SLOT(readClient()) );
}
void serverwindow::readClient()
{
//read the data obtained from client
ui->textEdit->append("\nreadClient");
QTcpSocket* clientSocket = (QTcpSocket*)sender();
//create new thread to handle the client request
clienthandler* clientThread = new clienthandler( clientSocket );
connect( clientThread,SIGNAL(finished()),
clientThread,SLOT(deleteLater()) );
clientThread->start();
clientThread->setPriority(QThread::HighestPriority);
}
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 -
void clienthandler::run()
{
clientConnSocket
->moveToThread
(QThread::currentThread());
if(clientConnSocket->canReadLine())
{
QString curData
(clientConnSocket
->readLine
());
if ( tokens[0] == "GET" )
{
//try to send a file's contents
//1. Small sized html file
QFile htmlfile
("c:\\server_files\\index.html");
return;
QString content_type
= "video/mp4;";
//os.setAutoDetectUnicode(true);
os << "HTTP/1.0 200 Ok\r\n"
"Content-Type: "<< content_type <<"charset=\"utf-8\"\r\n"
"\r\n";
os.flush();
// Streaming the file
clientConnSocket->write(block);
}
}
clientConnSocket->disconnectFromHost();
clientConnSocket->close();
}
void clienthandler::run()
{
clientConnSocket->moveToThread(QThread::currentThread());
if(clientConnSocket->canReadLine())
{
QString curData(clientConnSocket->readLine());
QStringList tokens = curData.split(QRegExp("[ \r\n][ \r\n]*"));
if ( tokens[0] == "GET" )
{
//try to send a file's contents
//1. Small sized html file
QFile htmlfile("c:\\server_files\\index.html");
if (!htmlfile.open(QIODevice::ReadOnly))
return;
QString content_type = "video/mp4;";
QTextStream os( clientConnSocket );
//os.setAutoDetectUnicode(true);
os << "HTTP/1.0 200 Ok\r\n"
"Content-Type: "<< content_type <<"charset=\"utf-8\"\r\n"
"\r\n";
os.flush();
// Streaming the file
QByteArray block = htmlfile.readAll();
clientConnSocket->write(block);
}
}
clientConnSocket->disconnectFromHost();
clientConnSocket->close();
}
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.
Bookmarks