gpt4 book ai didi

QT QTcpServer::incomingConnection(qintptr handle) 没有触发?

转载 作者:可可西里 更新时间:2023-11-01 02:33:19 24 4
gpt4 key购买 nike

我第一次尝试使用 Qt 创建多线程服务器。通常情况下,我们会使用 QTcpServer::nextPendingConnection() 返回的套接字指针,套接字句柄已经烘焙 - 但由于我在单独的线程上与连接客户端交互,我需要创建套接字分别使用来自 QTcpServer::incomingConnection(qintptr handle) 的 qintptr handle。在一个非常沉闷、充满错误的调试 session 之后,我设法将问题追查到 QTcpServer::incomingConnection() 从未被解雇?

有没有人遇到过类似的问题 - 最近版本的 Qt 有什么变化吗?

这些是我试过的:

  • QTcpServer::incomingConnection(qintptr handle)
  • QTcpServer::incomingConnection(qintptr socketDescriptor)
  • QTcpServer::incomingConnection(int handle)

编辑:

创建服务器实例:

TestServer *myServer = new TestServer();
myServer->initializeServer(1234);

调用:

void TestServer::initializeServer(quint16 port)
{

mainServer = new QTcpServer(this);

mainServer->listen(QHostAddress::Any, port);

qDebug() << "Listening for connections on port: " << port;



}

服务器正在监听。当客户端连接 incomingConnection(qintptr handle) 时应该调用:

void TestServer::incomingConnection(qintptr socketDescriptor){

TestClient *client = new TestClient(this);
client->setSocket(socketDescriptor);

}

调用:

void TestClient::setSocket(quint16 socketDescr)
{
socket = new QTcpSocket(this);

socket->setSocketDescriptor(socketDescr);

connect(socket, SIGNAL(connected()),this,SLOT(connected()));
connect(socket, SIGNAL(disconnected()),this,SLOT(disconnected()));
connect(socket, SIGNAL(readyRead()),this,SLOT(readyRead()));



}

调用 connect() 信号:

void TestClient::connected()
{
qDebug() << "Client connected..."; // This debug never appears in the console, since QTcpServer::incomingConnection isn't being fired.

}

最佳答案

您的代码有一些错误:

  1. TestServer 处,您的QTcpServer 可能已聚合,但您需要继承它。在这种情况下,您尝试覆盖 incomingConnection() 方法,但您没有基类,您只是创建新的 incomingConnection(),而不是覆盖。
  2. 您从 incomingConnection() 获取了 qintptr descriptor 变量,但是在 setSocket() 方法中设置了 quint16 类型.
  3. 您可能会混合使用服务器的客户端和您自己的客户端,后者仅获取传入连接并处理套接字数据。

我在下面写了一些小例子来帮助你理解 tcp 客户端-服务器通信。

服务器部分

主要部分是服务器本身:

#include <QTcpServer>

class TestServer: public QTcpServer
{
public:
TestServer(QObject *parent = 0);
void incomingConnection(qintptr handle) Q_DECL_OVERRIDE;
};

看看:我不加总QTcpServer ,却继承了它。在这种情况下,您可以覆盖 incomingConnection()方法正确。在构造函数中,我们只是使用 listen() 启动服务器进行监听。方法:

TestServer::TestServer(QObject *parent):
QTcpServer(parent)
{
if (this->listen(QHostAddress::Any, 2323)) {
qDebug() << "Server start at port: " << this->serverPort();
} else {
qDebug() << "Start failure";
}
}

然后是重写 incomingConnection() 的时候了:

void TestServer::incomingConnection(qintptr handle)
{
qDebug() << Q_FUNC_INFO << " new connection";
SocketThread *socket = new SocketThread(handle);

connect(socket, SIGNAL(finished()), socket, SLOT(deleteLater()));
socket->start();
}

我创建了处理传入数据的 SocketThread 对象:

#include <QThread>
#include <QObject>

class QTcpSocket;

class SocketThread: public QThread
{
Q_OBJECT
public:
SocketThread(qintptr descriptor, QObject *parent = 0);
~SocketThread();

protected:
void run() Q_DECL_OVERRIDE;

private slots:
void onConnected();
void onReadyRead();
void onDisconnected();

private:
QTcpSocket *m_socket;
qintptr m_descriptor;
};

我们继承自QThread为了使我们的服务器多线程,所以我们必须覆盖 run()方法:

SocketThread::SocketThread(qintptr descriptor, QObject *parent)
: QThread(parent), m_descriptor(descriptor)
{

}

void SocketThread::run()
{
qDebug() << Q_FUNC_INFO;
m_socket = new QTcpSocket;
m_socket->setSocketDescriptor(m_descriptor);

connect(m_socket, SIGNAL(readyRead()), this, SLOT(onReadyRead()), Qt::DirectConnection);
connect(m_socket, SIGNAL(disconnected()), this, SLOT(onDisconnected()), Qt::DirectConnection);

exec();
}

这样我们就可以初始化我们的 QTcpSocket ,设置套接字描述符,将其连接到readyRead()disconnected()信号并启动事件循环。

void SocketThread::onReadyRead()
{
QDataStream in(m_socket);
in.setVersion(QDataStream::Qt_5_5);

QString message;
in >> message;
qDebug() << message;

m_socket->disconnectFromHost();
}

void SocketThread::onDisconnected()
{
m_socket->close();
// Exit event loop
quit();
}

onReadyRead() 时,只需从客户端读取一些QString,将其写入控制台并断开与主机的连接。在 onDisconnected() 我们 close套接字连接和退出事件循环。

客户端

这只是示例和难闻的风格,但我在 QPushButton::clicked 信号上的 MainWindow 类中创建了与服务器的连接:

void MainWindow::on_pushButton_clicked()
{
QTcpSocket *client = new QTcpSocket;
connect(client, SIGNAL(connected()), this, SLOT(connected()));
connect(client, SIGNAL(disconnected()), client, SLOT(deleteLater()));

client->connectToHost(QHostAddress::LocalHost, 2323);
client->waitForConnected();

if (client->state() != QAbstractSocket::ConnectedState ) {
qDebug() << Q_FUNC_INFO << " can't connect to host";
delete client;
return;
}
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_5_5);
out << QString("Hello");
out.device()->seek(0);
client->write(block);
}

void MainWindow::connected()
{
qDebug() << Q_FUNC_INFO << " client connected";
}

我创建了新的 QTcpSocket,将其连接到信号并尝试连接到主机(在我的例子中是本地主机)。等待连接并检查套接字状态。如果一切正常,我将写入套接字 QString - 只是示例。

这是组织多线程客户端-服务器架构的可能方法之一,我希望它对您有所帮助,您会发现自己的错误。

关于QT QTcpServer::incomingConnection(qintptr handle) 没有触发?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31720030/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com