我试图弄清楚如何正确使用Qt TCP套接字以及多线程。我想,作为一个支持更复杂但类似的测试项目,我将在未来尝试,执行以下操作:一个简单的应用程序,要么只侦听一个传入连接,要么连接到serversocket;接下来它打印通过该套接字接收的所有内容。
我连接到serversocket的情况(我使用带有-l选项的netcat)可以正常工作:netcat发送到我的应用程序的所有内容都正确打印。但是,当我使用我的程序来侦听那个传入连接(由netcat生成)时,连接成功但是我得到了这个运行时错误:
QObject::connect: No such signal QThread::readyRead() in ..\QTcpTest\listener.cpp:8
这是整个代码(不介意我有时使用的普通C I / O,我稍后会删除它):
[main.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;
}
[peer.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
[peer.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();
}
[listener.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
[listener.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实例创建套接字时,而不是在其他情况下。我错过了什么?
看我自己的答案。
答案 0 :(得分:0)
我不再使用额外的线程(请参阅注释)。一旦建立连接,问题就是删除Peer::acceptPeer()
中的QTcpServer实例。我以为我可以删除它,因为我只需要一个套接字QTcpServer::nextPendingConnection()
返回。显然这是错误的,套接字也可能被破坏,留给我一个流氓指针(我认为)崩溃程序。现在我只有在收到套接字后才调用_server->pauseAccepting();
,一切正常。
以下是我所做的改变:
void Peer::acceptPeer()
{
disconnect(this, SLOT(acceptPeer()));
if (_isHost)
{
_socket = _server->nextPendingConnection();
_server->pauseAccepting(); // no deletion
}
_listener = new Listener(_socket);
}