用qt写一段socket代理层的代码实现两个上位机同时连接两个下位机,并将两个下位机的数据呈现在上位机上
时间: 2024-03-20 12:42:37 浏览: 12
下面是一个示例代码,它实现了两个上位机同时连接两个下位机,并将两个下位机的数据呈现在上位机上。这个示例使用Qt的QTcpSocket和QTcpServer类来实现Socket通信。
```cpp
#include <QTcpServer>
#include <QTcpSocket>
#include <QDataStream>
#include <QDebug>
class Proxy : public QObject
{
Q_OBJECT
public:
Proxy(QObject *parent = nullptr) : QObject(parent)
{
// 创建两个下位机的连接
m_downstream1 = new QTcpSocket(this);
m_downstream2 = new QTcpSocket(this);
// 监听两个上位机的连接
m_upstream1 = new QTcpServer(this);
m_upstream2 = new QTcpServer(this);
connect(m_upstream1, &QTcpServer::newConnection, this, &Proxy::onUpstream1Connected);
connect(m_upstream2, &QTcpServer::newConnection, this, &Proxy::onUpstream2Connected);
}
private slots:
void onUpstream1Connected()
{
auto clientSocket = m_upstream1->nextPendingConnection();
connect(clientSocket, &QTcpSocket::readyRead, this, &Proxy::onUpstream1DataReady);
m_upstream1Clients.append(clientSocket);
}
void onUpstream1DataReady()
{
auto clientSocket = qobject_cast<QTcpSocket*>(sender());
if (clientSocket == nullptr) {
return;
}
// 读取数据并转发到下位机1
QByteArray data = clientSocket->readAll();
m_downstream1->write(data);
}
void onUpstream2Connected()
{
auto clientSocket = m_upstream2->nextPendingConnection();
connect(clientSocket, &QTcpSocket::readyRead, this, &Proxy::onUpstream2DataReady);
m_upstream2Clients.append(clientSocket);
}
void onUpstream2DataReady()
{
auto clientSocket = qobject_cast<QTcpSocket*>(sender());
if (clientSocket == nullptr) {
return;
}
// 读取数据并转发到下位机2
QByteArray data = clientSocket->readAll();
m_downstream2->write(data);
}
void onDownstream1Connected()
{
qDebug() << "Downstream 1 connected";
}
void onDownstream1DataReady()
{
QByteArray data = m_downstream1->readAll();
for (auto client : m_upstream1Clients) {
client->write(data);
}
}
void onDownstream2Connected()
{
qDebug() << "Downstream 2 connected";
}
void onDownstream2DataReady()
{
QByteArray data = m_downstream2->readAll();
for (auto client : m_upstream2Clients) {
client->write(data);
}
}
private:
QTcpSocket *m_downstream1 = nullptr;
QTcpSocket *m_downstream2 = nullptr;
QTcpServer *m_upstream1 = nullptr;
QTcpServer *m_upstream2 = nullptr;
QList<QTcpSocket*> m_upstream1Clients;
QList<QTcpSocket*> m_upstream2Clients;
};
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Proxy proxy;
proxy.connect(proxy.m_downstream1, &QTcpSocket::connected, &proxy, &Proxy::onDownstream1Connected);
proxy.connect(proxy.m_downstream1, &QTcpSocket::readyRead, &proxy, &Proxy::onDownstream1DataReady);
proxy.connect(proxy.m_downstream2, &QTcpSocket::connected, &proxy, &Proxy::onDownstream2Connected);
proxy.connect(proxy.m_downstream2, &QTcpSocket::readyRead, &proxy, &Proxy::onDownstream2DataReady);
// 启动两个上位机的监听
proxy.m_upstream1->listen(QHostAddress::Any, 8080);
proxy.m_upstream2->listen(QHostAddress::Any, 8081);
return a.exec();
}
```
这段代码中,我们创建了一个名为Proxy的类,它继承自QObject,用于实现Socket代理的功能。在类的构造函数中,我们创建了两个下位机的连接,以及两个上位机的监听器(m_upstream1和m_upstream2)。
在onUpstream1Connected和onUpstream2Connected方法中,我们创建了两个上位机的连接,并将它们的readyRead信号连接到onUpstream1DataReady和onUpstream2DataReady方法中。在这两个方法中,我们读取上位机发送的数据,并将它们转发到下位机1和下位机2。
在onDownstream1Connected和onDownstream2Connected方法中,我们输出调试信息以表明下位机已经连接上了。在onDownstream1DataReady和onDownstream2DataReady方法中,我们读取下位机1和下位机2发送的数据,并将它们转发到所有连接到m_upstream1和m_upstream2的上位机。
最后,在main函数中,我们创建了一个Proxy对象,并连接了下位机1和下位机2的connected和readyRead信号到onDownstream1Connected、onDownstream1DataReady、onDownstream2Connected和onDownstream2DataReady方法中。我们还启动了两个上位机的监听器,使得它们可以接受连接。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)