我第一次尝试使用Qt创建一个多线程服务器。通常会使用QTcpServer::nextPendingConnection()
返回的套接字指针,其中套接字句柄已经被烘焙 - 但由于我在一个单独的线程上与连接客户端连接,我需要使用{{1分别创建套接字来自QTcpServer :: incomingConnection(qintptr句柄)。在一个非常沉闷,错误打包的调试会话之后,我设法追踪到qintptr handle
从未被解雇的问题?
有没有人遇到过类似的问题 - 最新版Qt有什么变化吗?
这些是我尝试过的:
QTcpServer::incomingConnection()
QTcpServer::incomingConnection(qintptr handle)
QTcpServer::incomingConnection(qintptr socketDescriptor)
编辑:
创建服务器实例:
QTcpServer::incomingConnection(int handle)
哪个电话:
TestServer *myServer = new TestServer();
myServer->initializeServer(1234);
服务器正在侦听。当客户端连接incomingConnection(qintptr句柄)应该被调用:
void TestServer::initializeServer(quint16 port)
{
mainServer = new QTcpServer(this);
mainServer->listen(QHostAddress::Any, port);
qDebug() << "Listening for connections on port: " << port;
}
哪个电话:
void TestServer::incomingConnection(qintptr socketDescriptor){
TestClient *client = new TestClient(this);
client->setSocket(socketDescriptor);
}
调用connect()信号:
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()));
}
答案 0 :(得分:9)
您的代码中存在一些错误:
TestServer
您的QTcpServer
可能已汇总,但您需要继承它。在这种情况下,您尝试覆盖incomingConnection()
方法,但您没有基类,只需创建新的incomingConnection()
,而不是覆盖。qintptr descriptor
获得incomingConnection()
变量,但在quint16
方法设置setSocket()
类型。我在下面写了一些小例子,以便您理解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套接字连接和退出事件循环。
客户端部分
这只是示例和糟糕的风格,但我在MainWindow
类QPushButton::clicked
上创建了与服务器的连接信号:
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
,将其连接到信号并尝试连接到主机(在我的情况下,它是localhost)。等待连接并检查插座状态。如果一切正常,我会写入套接字QString
- 只是示例。
这是组织多线程客户端 - 服务器架构的可能方法之一,我希望它对您有所帮助,并找到您的错误。