我试图弄清楚如何正确使用 Qt TCP 套接字以及多线程。作为一个支持更复杂但类似的东西的测试项目,我希望在未来尝试执行以下操作:一个简单的应用程序,它要么只监听一个传入连接,要么连接到服务器套接字;接下来,它会打印通过该套接字接收到的所有内容。
我连接到服务器套接字的情况(为此我使用带有 -l 选项的 netcat)工作正常:netcat 发送到我的应用程序的所有内容都被正确打印。但是,当我使用我的程序侦听一个传入连接(由 netcat 生成)时,连接成功,但随后出现此运行时错误:
QObject::connect: No such signal QThread::readyRead() in ..\QTcpTest\listener.cpp:8
这是整个代码(不要介意有时使用普通的 CI/OI,我稍后会删除它):
[主.cpp]
#include "peer.h"
#include <QCoreApplication>
#include <QThread>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static bool askIfServer();
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Peer *p;
int rval;
if (askIfServer())
p = new Peer;
else
p = new Peer(false);
rval = a.exec();
delete p;
return rval;
}
static bool askIfServer()
{
bool isServer;
char ibuf[BUFSIZ];
fputs("Choose \'host\' or \'join\': ", stderr);
fgets(ibuf, BUFSIZ, stdin);
ibuf[strlen(ibuf) - 1] = '\0';
fflush(stdin);
if (strcmp(ibuf, "host") == 0)
isServer = true;
else if (strcmp(ibuf, "join") == 0)
isServer = false;
else
{
fputs("Failure.\n", stderr);
exit(-1);
}
return isServer;
}
[对等.h]
#ifndef PEER_H
#define PEER_H
#include "listener.h"
#include <QObject>
#include <QThread>
#include <QtNetwork/QTcpSocket>
#include <QtNetwork/QTcpServer>
class Peer : public QObject
{
Q_OBJECT
public:
static const quint16 PORT = 5483;
explicit Peer(bool isHost = true, QString hostname = "localhost", QObject *parent = 0);
public slots:
void acceptPeer();
private:
bool _isHost;
QTcpServer *_server;
QTcpSocket *_socket;
QThread *_lThread;
Listener *_listener;
};
#endif // PEER_H
[对等.cpp]
#include "peer.h"
Peer::Peer(bool isHost, QString hostname, QObject *parent) :
QObject(parent),
_isHost(isHost)
{
if (_isHost)
{
_server = new QTcpServer;
connect(_server, SIGNAL(newConnection()), this, SLOT(acceptPeer()));
_server->listen(QHostAddress::Any, PORT);
}
else
{
_socket = new QTcpSocket;
connect(_socket, SIGNAL(hostFound()), this, SLOT(acceptPeer()));
_socket->connectToHost(hostname, PORT);
}
}
void Peer::acceptPeer()
{
disconnect(this, SLOT(acceptPeer()));
if (_isHost)
{
_socket = _server->nextPendingConnection();
delete _server;
_server = NULL;
}
_lThread = new QThread;
_listener = new Listener(_socket);
_listener->moveToThread(_lThread);
_lThread->start();
}
[监听器.h]
#ifndef LISTENER_H
#define LISTENER_H
#include <iostream>
#include <QObject>
#include <QByteArray>
#include <QtNetwork/QTcpSocket>
using std::cout;
class Listener : public QObject
{
Q_OBJECT
public:
explicit Listener(QTcpSocket *socket, QObject *parent = 0);
public slots:
void acceptData();
private:
QTcpSocket *_socket;
};
#endif // LISTENER_H
[监听器.cpp]
#include "listener.h"
Listener::Listener(QTcpSocket *socket, QObject *parent) :
QObject(parent),
_socket(socket)
{
// I guess this is where it goes wrong
connect(_socket, SIGNAL(readyRead()), this, SLOT(acceptData()), Qt::QueuedConnection);
}
void Listener::acceptData()
{
QByteArray data = _socket->readAll();
cout << data.constData();
}
我没有得到的是错误消息声称我尝试连接来自 QThread 的一些信号(我显然没有:参见 listener.cpp:8);更奇怪的是,这只发生在 QTcpServer 实例创建套接字时,而不是在其他情况下。我错过了什么?
编辑:已解决
看我自己的答案。