用qt写一段socket代理层的代码实现两个上位机同时连接两个下位机,并将两个下位机的数据呈现在上位机上
时间: 2024-03-20 12:41:31 浏览: 9
下面是一个用Qt编写的简单的Socket代理层的代码,实现两个上位机同时连接两个下位机,并将两个下位机的数据呈现在上位机上。假设下位机的地址分别为192.168.1.100和192.168.1.200,端口号为6666和7777。
```cpp
#include <QtNetwork>
#include <QTcpSocket>
class Proxy : public QObject
{
Q_OBJECT
public:
explicit Proxy(QObject *parent = nullptr)
: QObject(parent)
, m_server1(new QTcpServer(this))
, m_server2(new QTcpServer(this))
{
connect(m_server1, &QTcpServer::newConnection, this, &Proxy::onNewConnection1);
connect(m_server2, &QTcpServer::newConnection, this, &Proxy::onNewConnection2);
if (!m_server1->listen(QHostAddress::Any, 6666)) {
qDebug() << "Server1 listen failed";
}
if (!m_server2->listen(QHostAddress::Any, 7777)) {
qDebug() << "Server2 listen failed";
}
}
private slots:
void onReadyRead1()
{
QTcpSocket *socket = qobject_cast<QTcpSocket*>(sender());
if (!socket) {
return;
}
QTcpSocket *peer = m_peers.value(socket);
if (!peer) {
return;
}
QByteArray data = socket->readAll();
peer->write(data);
}
void onReadyRead2()
{
QTcpSocket *socket = qobject_cast<QTcpSocket*>(sender());
if (!socket) {
return;
}
QTcpSocket *peer = m_peers.value(socket);
if (!peer) {
return;
}
QByteArray data = socket->readAll();
peer->write(data);
}
void onNewConnection1()
{
QTcpSocket *socket = m_server1->nextPendingConnection();
if (!socket) {
return;
}
QTcpSocket *peer = new QTcpSocket(this);
peer->connectToHost(QHostAddress("192.168.1.100"), 6666);
if (!peer->waitForConnected()) {
qDebug() << "Connect to server1 failed";
socket->disconnectFromHost();
socket->deleteLater();
return;
}
m_peers.insert(socket, peer);
m_peers.insert(peer, socket);
connect(socket, &QTcpSocket::readyRead, this, &Proxy::onReadyRead1);
connect(peer, &QTcpSocket::readyRead, this, &Proxy::onReadyRead1);
connect(socket, &QTcpSocket::disconnected, this, &Proxy::onDisconnected);
connect(peer, &QTcpSocket::disconnected, this, &Proxy::onDisconnected);
}
void onNewConnection2()
{
QTcpSocket *socket = m_server2->nextPendingConnection();
if (!socket) {
return;
}
QTcpSocket *peer = new QTcpSocket(this);
peer->connectToHost(QHostAddress("192.168.1.200"), 7777);
if (!peer->waitForConnected()) {
qDebug() << "Connect to server2 failed";
socket->disconnectFromHost();
socket->deleteLater();
return;
}
m_peers.insert(socket, peer);
m_peers.insert(peer, socket);
connect(socket, &QTcpSocket::readyRead, this, &Proxy::onReadyRead2);
connect(peer, &QTcpSocket::readyRead, this, &Proxy::onReadyRead2);
connect(socket, &QTcpSocket::disconnected, this, &Proxy::onDisconnected);
connect(peer, &QTcpSocket::disconnected, this, &Proxy::onDisconnected);
}
void onDisconnected()
{
QTcpSocket *socket = qobject_cast<QTcpSocket*>(sender());
if (!socket) {
return;
}
QTcpSocket *peer = m_peers.value(socket);
if (!peer) {
return;
}
m_peers.remove(socket);
m_peers.remove(peer);
socket->deleteLater();
peer->deleteLater();
}
private:
QTcpServer *m_server1;
QTcpServer *m_server2;
QHash<QTcpSocket*, QTcpSocket*> m_peers;
};
```
这个代码中,我们创建了一个`Proxy`类,用来处理两个上位机和两个下位机之间的通信。在构造函数中,我们创建了两个`QTcpServer`对象来监听两个不同的端口。当有新的连接到来时,我们会创建一个新的`QTcpSocket`对象来处理连接。然后,我们会尝试连接到下位机的地址和端口号。如果连接成功,我们会将两个`QTcpSocket`对象存储到一个哈希表中,以便在数据传输时使用。我们还连接了两个上位机和两个下位机的`readyRead`和`disconnected`信号,以便在需要时处理数据和连接断开。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)