Quantcast
Channel: QtWebEngine
Viewing all articles
Browse latest Browse all 13965

Asynchronous TcpServer Share Object between Threads

$
0
0
Hello everyone, I have a TcpServer up and running. As soon as a client connects I create a new socket and when the client is ready, I start my new Thread with the Task I want it to do. Now when I am ready executing and want to write my Task-result to the socket I have to work with Signal&Slots to make the Socket-Thread send my result. But what I want to do is, send the result directly from the Task-Thread. I tried passing the pointer to the socket to the Result-Thread and write to it, but this only gives me a Warning on runtime: QObject: Cannot create children for a parent that is in a different thread. (Parent is QNativeSocketEngine(0×11d7b10), parent’s thread is QThread(0xfa9f40), current thread is QThread(0×101cac0). So maybe you can help me avoid Signals&Slots here. So this is the Method for creating the Socket: void MyClient::CreatSocket(qintptr handle) {     socket = new QTcpSocket(this);       socket->setSocketDescriptor(handle);       connect(socket, SIGNAL(connected()), this, SLOT(connected()));     connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));     connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead())); } and here I create the new Task as soon as the Client is ready, and give it the socket. void MyClient::readyRead() {     task = new MyTask();     task->setAutoDelete (true);       task->SetSocket(socket);     task->SetFilename(FileName);       QThreadPool::globalInstance()->start(task); } This is the Set Socket Function: void MyTask::SetSocket(QTcpSocket *socket) {     socket = socket; } and last but not least the run Method of MyTask: void MyTask::run() {     QString non = "This is Sparta!!!!";       if(socket->isWritable())     {         int written = socket->write(non.toUtf8());         if(written == -1)         {             qWarning() << "Data could not be written to Socket!";         }         else         {             qDebug()<<"Non written to Socket";         }     }     else     {         qWarning() << "Socket is not writable!";     } Thank you in advance! :)

Viewing all articles
Browse latest Browse all 13965

Trending Articles