我坚持使用有关线程qt网络的教程(在这里:http://doc.qt.io/qt-5/qtnetwork-threadedfortuneserver-example.html),我做了一些小的更改并将其集成到我的主程序中。但是incomingConnection()
永远不会执行,另一方面客户端可以进行连接。由于我想使用incomingConnection(),因此不再使用SIGNAL(newConnection())
,但即使这样也无法正常工作。
有人知道出什么事了吗?
这是我的.h
#include <QtNetwork>
#include <QTcpServer>
#include <QTcpSocket>
#include <QThread>
class WirelessNetThread: public Thread
{
Q_OBJECT
public:
WirelessNetThread(int socketDescriptor, QObject * parent);
void run() Q_DECL_OVERRIDE;
signals:
void error(QTcpSocket::SocketError socketError);
private:
int socketDescriptor;
QString text;
};
class WirelessNet : public QTcpServer
{
Q_OBJECT
public:
WirelessNet(QObject *parent = 0);
protected:
void incomingConnection(qintptr socketDescriptor) Q_DECL_OVERRIDE;
};
和
.cpp
WirelessNetThread::WirelessNetThread(int socketDescriptor, QObject *parent):QThread(parent), socketDescriptor(socketDescriptor)
{
}
void WirelessNetThread::run()
{
QTcpSocket tcpSocket;
if ( !tcpSocket.setSocketDescriptor(socketDescriptor))
{
emit error(tcpSocket.error());
return;
}
tcpSocket.disconnectFromHost();
tcpSocket.waitForDisconnected();
}
WirelessNet::WirelessNet(QObject *parent): QTcpServer(0)
{
listen(QHostAddress::Any, 5220);
printf("is listening %d\n", this->isListening());
}
void WirelessNet::incomingConnection(qintptr socketDescriptor)
{
qDebug() << "incomming \n";
printf("incomming \n");
WirelessNetThread *thread = new WirelessNetThread(socketDescriptor, this);
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();
}
这是我的主程序的摘录,它是从那里开始的(顺便说一句,如果我不包括moveToThread(),则没有关系:
WirelessNet *wifi = new WirelessNet(this->parent());
wifi->moveToThread(this->thread());
如果在wifi初始化后添加以下行,即使这样也没有影响:
wifi = new WirelessNet(this->parent());
QEventLoop testLoop;
testLoop.exec();
换句话说,“输入”永远不会被打印出来,因此我无法继续工作。有谁知道,这几乎是本教程中1:1的代码,这让我感到困惑。
最佳答案
在您的主要代码中:
WirelessNet *wifi = new WirelessNet(0); // 0 = assign no parent
QThread *wifiThread = new QThread;
wifi->moveToThread(wifiThread);
QObject::connect(wifiThread, SIGNAL(started()), wifi, SLOT(startWifi()));
// start() will start its own event loop, it will emit started(), therefore startWifi() slot will be called.
wifiThread->start();
然后是您的WirelessNet类 header :
class WirelessNet : public QTcpServer
{
Q_OBJECT
public:
WirelessNet(QObject *parent = 0);
protected:
void incomingConnection(qintptr socketDescriptor) Q_DECL_OVERRIDE;
public slots:
void startWifi();
};
然后是您的WirelessNet类主体:
WirelessNet::WirelessNet(QObject *parent) :
QTcpServer(parent)
{
// Do nothing much here because we want to initialise new stuff in our thread.
// When this function runs we have not moved this to the new thread - or even started it.
}
void WirelessNet::incomingConnection(qintptr socketDescriptor)
{
qDebug() << "incomming \n";
printf("incomming \n");
WirelessNetThread *thread = new WirelessNetThread(socketDescriptor, this);
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();
}
// Called when the thread has started
void WirelessNet::startWifi()
{
// Anything done here is now safely within out new thread.
listen(QHostAddress::Any, 5220);
printf("is listening %d\n", this->isListening());
}
注释这是示例代码,我直接将其写到堆栈溢出中,但尚未编译,因此可能存在一些错误:)我已经评论了一些关键点,您在原始尝试中可能出错了。
关于c++ - QtNetwork:为什么我没有检测到传入的连接? (永远不会触发传入的Connection()),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36195654/