Friday, 27 September 2013

QT write to tcpSocket fails with different thread error

QT write to tcpSocket fails with different thread error

I have created a simple threaded TCP server which collects 3 lines read
from the socket, and then tries to echo them back to the socket. The
function below crashes
void MyServer::echoCommand()
{
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
for ( QStringList::Iterator it = commandList.begin(); it !=
commandList.end(); ++it ) {
out << "Command: " << *it << endl;
}
out << endl;
tcpSocketPtr->write(block);
tcpSocketPtr->disconnectFromHost();
tcpSocketPtr->waitForDisconnected();
}
as soon as I try to write to the socket, with this error:
QObject: Cannot create children for a parent that is in a different
thread. (Parent is QNativeSocketEngine(0x7f19cc002720), parent's thread is
FortuneThread(0x25411d0), current thread is QThread(0x220ff90)
Since I create the tcpSocketPtr in the run() function, I know it is in the
same thread as this function. Why would the socket write operation fail?

No comments:

Post a Comment