Hello,
I am new to QT.
In my project I have QTcpServer with multithreading. I need to implement that when server recieve something from one client(client is GUI aplication), send it to all clients except the one who sent data. Problem occurs when I call socket->write(data) in function sendToAllClients() in server.cpp. I got following:
QObject:Cannot create children for the parent that is in different thread qt.
Could anyone please help me with this. I was searching about on google, but whatever I tried it doesn't work.
Thanks a lot.
My code is below:
mythread.cpp
MyThread
::MyThread(qintptr ID,
QObject *parent
) :{
this->socketDescriptor = ID;
this->disconnectedSocket=-1;
}
void MyThread::run()
{
if(!socket->setSocketDescriptor(this->socketDescriptor))
{
emit error(socket->error());
return;
}
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()), Qt::DirectConnection);
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
exec();
}
void MyThread::readyRead()
{
readyToRead=true;
if(readyToRead==true)
emit ReadyToReadAllSockets(); //ReadyToReadAllSockets() is signal I wrote to make connection between thread and server.
}
MyThread::MyThread(qintptr ID, QObject *parent) :
QThread(parent)
{
this->socketDescriptor = ID;
this->disconnectedSocket=-1;
}
void MyThread::run()
{
socket = new QTcpSocket();
if(!socket->setSocketDescriptor(this->socketDescriptor))
{
emit error(socket->error());
return;
}
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()), Qt::DirectConnection);
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
exec();
}
void MyThread::readyRead()
{
readyToRead=true;
if(readyToRead==true)
emit ReadyToReadAllSockets(); //ReadyToReadAllSockets() is signal I wrote to make connection between thread and server.
}
To copy to clipboard, switch view to plain text mode
server.cpp
{
connect(this,SIGNAL(newConnection()),this,SLOT(numOfClients()));
}
void Server::incomingConnection(qintptr socketDescriptor)
{
clients_list.append(socketDescriptor);
MyThread *thread = new MyThread(socketDescriptor, this);
threads.append(thread);
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
connect(thread, SIGNAL(finished()), this, SLOT(clientDisconnect()));
connect(thread, SIGNAL(finished()), this, SLOT(removeClientFromList()));
connect(thread, SIGNAL(ReadyToReadAllSockets()), this, SLOT(sendToAllClients()));
thread->start();
}
void Server::sendToAllClients()
{
qDebug()<< "Writing in sockets....";
MyThread* thread;
for(int i=0;i<threads.size();i++)
{
if(threads.at(i)->getIfReady())
{
thread=threads.at(i);
Data = threads.at(i)->getSocket()->readAll();
threads.at(i)->setReady(false);
}
}
for(int i=0;i<threads.size();i++)
{
if(threads.at(i)!=thread)
{
socket=threads.at(i)->getSocket();
socket->write(Data); // this is where occur error
}
}
}
Server::Server(QObject *parent) :
QTcpServer(parent)
{
connect(this,SIGNAL(newConnection()),this,SLOT(numOfClients()));
}
void Server::incomingConnection(qintptr socketDescriptor)
{
clients_list.append(socketDescriptor);
MyThread *thread = new MyThread(socketDescriptor, this);
threads.append(thread);
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
connect(thread, SIGNAL(finished()), this, SLOT(clientDisconnect()));
connect(thread, SIGNAL(finished()), this, SLOT(removeClientFromList()));
connect(thread, SIGNAL(ReadyToReadAllSockets()), this, SLOT(sendToAllClients()));
thread->start();
}
void Server::sendToAllClients()
{
qDebug()<< "Writing in sockets....";
MyThread* thread;
QByteArray Data;
for(int i=0;i<threads.size();i++)
{
if(threads.at(i)->getIfReady())
{
thread=threads.at(i);
Data = threads.at(i)->getSocket()->readAll();
threads.at(i)->setReady(false);
}
}
for(int i=0;i<threads.size();i++)
{
if(threads.at(i)!=thread)
{
socket=threads.at(i)->getSocket();
socket->write(Data); // this is where occur error
}
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks