用qt写一段代码实现两个上位机通过代理连接两个下位机,两个下位机收到命令后会发送的数据,发送的数据会在两台上位机上同时显示出来
时间: 2024-03-27 14:38:53 浏览: 49
高分项目,QT上位机,QT四轴上位机初级版,内含完整源代码+下位机协议
好的,这是一个比较复杂的问题,需要涉及到网络通信和图形界面编程。以下是一份简单的代码示例,供参考:
```cpp
#include <QTcpSocket>
#include <QTcpServer>
#include <QHostAddress>
#include <QDataStream>
#include <QByteArray>
#include <QApplication>
#include <QWidget>
#include <QTextEdit>
#include <QVBoxLayout>
class Proxy : public QObject
{
Q_OBJECT
public:
Proxy(QObject *parent = nullptr)
: QObject(parent)
{
// 创建两个下位机的连接
m_downstream1 = new QTcpSocket(this);
m_downstream2 = new QTcpSocket(this);
connect(m_downstream1, &QTcpSocket::readyRead, this, &Proxy::onDownstream1ReadyRead);
connect(m_downstream2, &QTcpSocket::readyRead, this, &Proxy::onDownstream2ReadyRead);
// 创建两个上位机的监听
m_upstreamServer1 = new QTcpServer(this);
m_upstreamServer2 = new QTcpServer(this);
connect(m_upstreamServer1, &QTcpServer::newConnection, this, &Proxy::onUpstream1NewConnection);
connect(m_upstreamServer2, &QTcpServer::newConnection, this, &Proxy::onUpstream2NewConnection);
// 启动监听
m_upstreamServer1->listen(QHostAddress::LocalHost, 1234);
m_upstreamServer2->listen(QHostAddress::LocalHost, 5678);
}
private slots:
void onUpstream1NewConnection()
{
// 当上位机1连接时,创建一个新的连接到下位机1
auto upstream1 = m_upstreamServer1->nextPendingConnection();
m_downstream1->connectToHost(QHostAddress::LocalHost, 4321);
connect(upstream1, &QTcpSocket::readyRead, [upstream1, this]() {
onUpstream1ReadyRead(upstream1);
});
}
void onUpstream2NewConnection()
{
// 当上位机2连接时,创建一个新的连接到下位机2
auto upstream2 = m_upstreamServer2->nextPendingConnection();
m_downstream2->connectToHost(QHostAddress::LocalHost, 8765);
connect(upstream2, &QTcpSocket::readyRead, [upstream2, this]() {
onUpstream2ReadyRead(upstream2);
});
}
void onUpstream1ReadyRead(QTcpSocket *upstream1)
{
// 当上位机1有数据可读时,将数据转发到下位机1
QByteArray data = upstream1->readAll();
m_downstream1->write(data);
}
void onUpstream2ReadyRead(QTcpSocket *upstream2)
{
// 当上位机2有数据可读时,将数据转发到下位机2
QByteArray data = upstream2->readAll();
m_downstream2->write(data);
}
void onDownstream1ReadyRead()
{
// 当下位机1有数据可读时,将数据转发到上位机1
QByteArray data = m_downstream1->readAll();
for (auto upstream1 : m_upstreamServer1->children()) {
auto socket = qobject_cast<QTcpSocket*>(upstream1);
if (socket) {
socket->write(data);
}
}
}
void onDownstream2ReadyRead()
{
// 当下位机2有数据可读时,将数据转发到上位机2
QByteArray data = m_downstream2->readAll();
for (auto upstream2 : m_upstreamServer2->children()) {
auto socket = qobject_cast<QTcpSocket*>(upstream2);
if (socket) {
socket->write(data);
}
}
}
private:
QTcpSocket *m_downstream1;
QTcpSocket *m_downstream2;
QTcpServer *m_upstreamServer1;
QTcpServer *m_upstreamServer2;
};
class MainWindow : public QWidget
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr)
: QWidget(parent)
{
// 创建两个文本框用于显示数据
m_textEdit1 = new QTextEdit(this);
m_textEdit2 = new QTextEdit(this);
// 创建布局
auto layout = new QVBoxLayout(this);
layout->addWidget(m_textEdit1);
layout->addWidget(m_textEdit2);
// 创建代理对象
m_proxy = new Proxy(this);
}
private slots:
void onDownstream1ReadyRead()
{
// 当下位机1有数据可读时,在文本框1中显示数据
QByteArray data = m_proxy->m_downstream1->readAll();
m_textEdit1->append(QString::fromUtf8(data));
}
void onDownstream2ReadyRead()
{
// 当下位机2有数据可读时,在文本框2中显示数据
QByteArray data = m_proxy->m_downstream2->readAll();
m_textEdit2->append(QString::fromUtf8(data));
}
private:
QTextEdit *m_textEdit1;
QTextEdit *m_textEdit2;
Proxy *m_proxy;
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow window;
window.show();
return app.exec();
}
#include "main.moc"
```
这段代码实现了一个简单的代理服务器,它监听两个端口,分别用于接收两个上位机的连接。当上位机连接时,代理服务器会创建一个新的连接到相应的下位机,并将数据转发。同时,代理服务器会在两个文本框中显示收到的数据。请注意,这是一个简化的示例,实际情况可能更加复杂,需要根据具体的需求进行调整。
阅读全文