QtNetwork:为什么我检测不到传入的连接?(incomingConnection() 永远不会被触发)
QtNetwork: Why don't I detect incomming connections ? (incomingConnection() is never fired)
我坚持学习关于线程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 %dn", 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();
换句话说,"incoming"永远不会打印出来,所以我无法继续工作。有人知道吗,这几乎是教程中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类标题:
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 %dn", this->isListening());
}
注意这是示例代码,我直接将其写入堆栈溢出,但尚未编译,因此可能存在一些错误:)我已经评论了一些关键点,您可能在最初的尝试中出错了。
相关文章:
- 为什么是谷神星协方差.计算()似乎永远运行而不返回?
- 密码登录程序将永远循环并显示不正确的结果
- 我想在C++中读取一些多个字符,但它永远不会读取第二个字符
- SFML 碰撞永远不会在我的系统中注册
- 先进先出:一个进程永远不会从管道读取
- 移动构造函数永远不会被调用
- 程序永远不会进入虚拟析构函数
- 在 *.cpp 文件中实现的 c++ 函数/方法永远不会内联扩展吗?
- 指针永远不会在链表深层复制构造函数中达到 null
- 为什么我的 while 循环永远不会在 C++ 中的数字猜测游戏中结束?
- 类似函数的宏参数名称替换是否保证永远不会发生?
- curl_slist->next 永远不会为空,因此当循环卡住时
- 我应该使用无符号字符而不是 int 来存储永远不会大于 255 的值吗?
- cudaMemcpyToSymbol只是挂起,永远不会返回.GPU 处理速度为 100%.代码在 K40 上工作正常,但
- 暂停功能,它永远循环
- 字符串迭代器永远不会达到 std::string::end()
- C++多线程应用程序将永远挂起
- Linux 从永远在线的应用程序中创建和删除文件
- 为什么我的线程永远不会结束
- 无法从串行端口读取.WaitCommEvent() 永远不会返回