我正在尝试第一次使用Qt创建一个多线程服务器.通常会使用QTcpServer::nextPendingConnection()已经烘焙的套接字句柄返回的套接字指针- 但由于我在单独的线程上与连接客户端连接,我需要使用qintptr handlefrom QTcpServer :: incomingConnection(qintptr handle)单独创建套接字).经过一个非常沉闷,错误打包的调试会话后,我设法将问题追踪到了QTcpServer::incomingConnection()永远不会被解雇?
有没有人有类似的问题 - 最新版本Qt有什么变化?
这些是我尝试过的:
QTcpServer::incomingConnection(qintptr handle)QTcpServer::incomingConnection(qintptr socketDescriptor)QTcpServer::incomingConnection(int handle)编辑:
创建服务器实例:
TestServer *myServer = new TestServer();
myServer->initializeServer(1234);
Run Code Online (Sandbox Code Playgroud)
哪个电话:
void TestServer::initializeServer(quint16 port)
{
mainServer = new QTcpServer(this);
mainServer->listen(QHostAddress::Any, port);
qDebug() << "Listening for connections on port: " << port;
}
Run Code Online (Sandbox Code Playgroud)
服务器正在侦听.当客户端连接incomingConnection(qintptr句柄)应该被调用:
void TestServer::incomingConnection(qintptr socketDescriptor){
TestClient *client = new TestClient(this);
client->setSocket(socketDescriptor);
}
Run Code Online (Sandbox Code Playgroud)
哪个电话:
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()));
}
Run Code Online (Sandbox Code Playgroud)
在connect()信号上调用:
void TestClient::connected()
{
qDebug() << "Client connected..."; // This debug never appears in the console, since QTcpServer::incomingConnection isn't being fired.
}
Run Code Online (Sandbox Code Playgroud)
您的代码有一些错误:
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;
};
Run Code Online (Sandbox Code Playgroud)
看看:我没有加重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";
}
}
Run Code Online (Sandbox Code Playgroud)
然后覆盖的时间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();
}
Run Code Online (Sandbox Code Playgroud)
我创建了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;
};
Run Code Online (Sandbox Code Playgroud)
我们继承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();
}
Run Code Online (Sandbox Code Playgroud)
这样我们初始化我们的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();
}
Run Code Online (Sandbox Code Playgroud)
在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";
}
Run Code Online (Sandbox Code Playgroud)
我创建新的QTcpSocket,连接到信号并尝试连接到主机(在我的情况下,它是localhost).等待连接并检查插座状态.如果一切正常我会写入socket QString- 只是示例.
这是组织多线程客户端 - 服务器架构的可能方法之一,我希望它对您有所帮助,并找到您的错误.
| 归档时间: |
|
| 查看次数: |
4781 次 |
| 最近记录: |