我创建了一个线程,其中包含一个QTcpServer
接受任何传入连接的线程:
void Service::run() //Service class is a subclass of QThread
{
server->listen(QHostAddress::LocalHost, ServicePortNo);
// server is a private member of Service
while(server->waitForNewConnection(-1)){
QTcpSocket *socket = server->nextPendingConnection();
handle(socket); // This is a pure virtual function
}
}
在handle(QTcpSocket *socket)
:
// TimeDateService is subclass of Service
// implementation of pure virtual function handle()
void TimeDateService::handle(QTcpSocket *socket)
{
(new TimeDateSocketHandler(socket))->Start();
}
注意:TimeDateSocketHandler
是的子类,SocketHandler
本身SocketHandler
是的子类,QThread
如下所示:
void SocketHandler::run()
{
if(!socket->waitForReadyRead(WAIT_TIMEOUT))
{
socket->disconnectFromHost();
socket->close();
return;
}
QByteArray request = socket->readAll();
QByteArray response = Serve(request); // Serve is a pure virtual function
socket->write(response);
socket->waitForBytesWritten(WAIT_TIMEOUT);
socket->disconnectFromHost();
socket->close();
}
最后是TimeDateSocketHandler
QByteArray TimeDateSocketHandler::Serve(QByteArray request)
{
QByteArray response;
response.append(QTime::currentTime().toString().toUtf8());
response.append(QString(SEPARATOR).toUtf8());
response.append(QDate::currentDate().toString().toUtf8());
return response;
}
主功能:
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
TimeDateService timedateService;
//CalculatorService calculatorService;
//RemoteCMDService remoteCMDService;
timedateService.StartService();
//calculatorService.StartService();
//remoteCMDService.StartService();
return a.exec();
}
在main
函数中,我启动了TimeDateService
. 但是当我连接到服务器以检索时间和日期时,服务器会发送时间和日期,但是当TimeDateSocketHandler
想要关闭套接字时程序崩溃:
QCoreApplication::sendEvent 中的 ASSERT 失败:“无法将事件发送到其他线程拥有的对象。当前线程 3998779bf0。接收器 ''(类型为 'QNat iveSocketEngine')在线程 39985efcd0 中创建”,文件 kernel\qcoreapplicio n.cpp ,第 494 行
谁能帮帮我,请问我该如何解决这个问题,非常感谢