qt tcp多线程上位机通讯
时间: 2023-07-21 10:03:04 浏览: 125
在Qt中,可以使用QTcpSocket和QTcpServer类来实现TCP通信。为了实现多线程通信,我们可以使用Qt的QThread类。
下面是一个简单的示例代码,它演示了如何使用Qt实现TCP多线程上位机通信:
```cpp
// server.h
#ifndef SERVER_H
#define SERVER_H
#include <QObject>
#include <QTcpServer>
#include <QTcpSocket>
class Server : public QObject
{
Q_OBJECT
public:
explicit Server(QObject *parent = nullptr);
void startServer();
void stopServer();
signals:
void newMessage(QString message);
private slots:
void acceptConnection();
void readData();
void displayError(QAbstractSocket::SocketError socketError);
private:
QTcpServer *m_server;
QList<QTcpSocket *> m_clients;
};
#endif // SERVER_H
```
```cpp
// server.cpp
#include "server.h"
Server::Server(QObject *parent) : QObject(parent), m_server(nullptr)
{
m_server = new QTcpServer(this);
connect(m_server, SIGNAL(newConnection()), this, SLOT(acceptConnection()));
}
void Server::startServer()
{
if (!m_server->listen(QHostAddress::Any, 1234))
{
qDebug() << "Could not start server";
return;
}
qDebug() << "Server started!";
}
void Server::stopServer()
{
m_server->close();
foreach (QTcpSocket *client, m_clients)
{
client->close();
}
}
void Server::acceptConnection()
{
QTcpSocket *client = m_server->nextPendingConnection();
connect(client, SIGNAL(readyRead()), this, SLOT(readData()));
connect(client, SIGNAL(disconnected()), client, SLOT(deleteLater()));
m_clients.append(client);
qDebug() << "New client connected!";
}
void Server::readData()
{
QTcpSocket *client = qobject_cast<QTcpSocket *>(sender());
if (!client)
return;
QString message = QString::fromUtf8(client->readAll());
emit newMessage(message);
}
void Server::displayError(QAbstractSocket::SocketError socketError)
{
qDebug() << "Socket error:" << socketError;
}
```
```cpp
// client.h
#ifndef CLIENT_H
#define CLIENT_H
#include <QObject>
#include <QTcpSocket>
class Client : public QObject
{
Q_OBJECT
public:
explicit Client(QObject *parent = nullptr);
public slots:
void connectToServer(QString host, int port);
void disconnectFromServer();
void sendMessage(QString message);
signals:
void connected();
void disconnected();
void newMessage(QString message);
private slots:
void readData();
void displayError(QAbstractSocket::SocketError socketError);
private:
QTcpSocket *m_socket;
};
#endif // CLIENT_H
```
```cpp
// client.cpp
#include "client.h"
Client::Client(QObject *parent) : QObject(parent), m_socket(nullptr)
{
m_socket = new QTcpSocket(this);
connect(m_socket, SIGNAL(readyRead()), this, SLOT(readData()));
connect(m_socket, SIGNAL(connected()), this, SIGNAL(connected()));
connect(m_socket, SIGNAL(disconnected()), this, SIGNAL(disconnected()));
}
void Client::connectToServer(QString host, int port)
{
m_socket->connectToHost(host, port);
}
void Client::disconnectFromServer()
{
m_socket->close();
}
void Client::sendMessage(QString message)
{
m_socket->write(message.toUtf8());
}
void Client::readData()
{
QString message = QString::fromUtf8(m_socket->readAll());
emit newMessage(message);
}
void Client::displayError(QAbstractSocket::SocketError socketError)
{
qDebug() << "Socket error:" << socketError;
}
```
在这个示例中,Server类和Client类分别表示服务器和客户端。Server类在开始时会创建一个QTcpServer对象并开始监听来自任何地址的连接请求。每当一个新的连接请求到达时,Server类会创建一个QTcpSocket对象来处理该连接。然后,Server类将客户端的QTcpSocket对象添加到一个列表中,以便在以后可以轻松地访问所有客户端。
Client类在连接到服务器时会创建一个QTcpSocket对象,并尝试与指定的主机和端口号建立连接。一旦连接成功,客户端就可以发送消息到服务器。每当客户端收到服务器的消息时,它将发出newMessage信号。
你可以通过创建QThread类的子类来实现多线程通信。在run()方法中,可以使用Server对象或Client对象来处理通信。
阅读全文