When socket writes data i get the "QObject: Cannot create children for a parent that is in a different thread." error, but the data is transferred anyway, because client gets it. QTcpSocket has no parent. Could someone help me in solving this? (Qt 4.2)
Here is the run():
void XRServerThread::run()
{
//end if error occured
if(! m_socket->setSocketDescriptor(m_socket_descriptor))
{
//-32300 - xr transport error
emit error
(XR_TRANSPORT_ERROR,
QString("Socket error: %1") .arg(m_socket->error()));
return;
}
connect(m_socket, SIGNAL(readyRead()), this, SLOT(readFromSocket()));
// connect(m_socket, SIGNAL(disconnected()), m_socket,
//SLOT(deleteLater()));
//exit the thread after disconnection
connect(m_socket, SIGNAL(disconnected()), this, SLOT(quit()));
//start event loop
exec();
}
void XRServerThread::run()
{
m_socket = new QTcpSocket;
//end if error occured
if(! m_socket->setSocketDescriptor(m_socket_descriptor))
{
//-32300 - xr transport error
emit error(XR_TRANSPORT_ERROR, QString("Socket error: %1")
.arg(m_socket->error()));
return;
}
connect(m_socket, SIGNAL(readyRead()), this, SLOT(readFromSocket()));
// connect(m_socket, SIGNAL(disconnected()), m_socket,
//SLOT(deleteLater()));
//exit the thread after disconnection
connect(m_socket, SIGNAL(disconnected()), this, SLOT(quit()));
//start event loop
exec();
}
To copy to clipboard, switch view to plain text mode
.
.
.
Here is the place where error occurs (m_socket->write()):
void XRServerThread
::sendHttpResponse(int status_code,
const QString &reason,
{
//This is the body of the response:
output.append(resp);
headers.setContentLength(output.size());
//This is the headers:
head_out.
append(QString("HTTP/1.1 %1 %2\r\n") .
arg(QString::number(status_code
)).
arg(reason
));
//creating string representation of headerss
{
head_out.
append(QString("%1: %2\r\n").
arg(key
).
arg(headers.
value(key
)));
}
head_out.append("\r\n");
//Write the headers out:
m_socket->write(head_out);
//Write the body out:
m_socket->write(output);
//Close the connection we will not write anything else
m_socket->disconnectFromHost();
m_socket->waitForDisconnected();
}
void XRServerThread::sendHttpResponse(int status_code, const QString &reason,
QHttpHeader & headers, QString resp)
{
//This is the body of the response:
QByteArray output;
output.append(resp);
headers.setContentLength(output.size());
//This is the headers:
QByteArray head_out;
head_out.append(QString("HTTP/1.1 %1 %2\r\n")
.arg(QString::number(status_code)).arg(reason));
QStringList keys = headers.keys();
//creating string representation of headerss
foreach(QString key, keys)
{
head_out.append(QString("%1: %2\r\n").arg(key).arg(headers.value(key)));
}
head_out.append("\r\n");
//Write the headers out:
m_socket->write(head_out);
//Write the body out:
m_socket->write(output);
//Close the connection we will not write anything else
m_socket->disconnectFromHost();
m_socket->waitForDisconnected();
}
To copy to clipboard, switch view to plain text mode
Bookmarks