我第一次尝试使用Qt创建多线程服务器。通常,人们会使用QTcpServer::nextPendingConnection()返回的套接字指针,而套接字句柄已经放好了--但是由于我在一个单独的线程上与连接客户端进行接口,我需要使用QTcpServer::incomingConnection(qintptr句柄)中的qintptr handle分别创建套接字。经过一段非常沉闷、错误填充的调试会话之后,我成功地找到了这个问题,直到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句柄)时,应该调用:
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.
}发布于 2015-07-30 13:26:41
您的代码有一些错误:
TestServer,您的QTcpServer可能是聚合的,但是您需要继承它。在这种情况下,您尝试重写incomingConnection()方法,但是您没有基类,您只是创建新的incomingConnection(),而不是覆盖。qintptr descriptor获得incomingConnection()变量,但是在setSocket()方法中设置quint16类型。为了理解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(),我们关套接字连接和退出事件循环。
客户端部件
这只是一个示例和难闻的风格,但我在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,将它连接到信号,并尝试连接到主机(在我的例子中,它是本地主机)。等待连接并检查套接字状态。如果一切都是正确的,我写套接字QString -只是一个例子。
这是组织多线程客户机-服务器架构的一种可能的方式,我希望它能对您和您发现您的错误有所帮助。
https://stackoverflow.com/questions/31720030
复制相似问题