用qt写一个socket代理代码实现两个上位机同时连接两个下位机,并将下位机的数据在两台上位机上呈现出来
时间: 2024-03-20 18:41:06 浏览: 60
高分项目,QT上位机,QT四轴上位机初级版,内含完整源代码+下位机协议
下面是一个简单的 Qt socket 代理示例,可以实现两个上位机同时连接两个下位机,并将下位机的数据在两台上位机上呈现出来:
```c++
#include <QtNetwork>
#include <QtWidgets>
class SocketProxy : public QObject
{
Q_OBJECT
public:
SocketProxy(QObject *parent = nullptr)
: QObject(parent)
{
// 创建两个下位机连接
m_downstreamHost1 = new QTcpServer(this);
m_downstreamHost1->listen(QHostAddress::LocalHost, 1234);
connect(m_downstreamHost1, &QTcpServer::newConnection, this, &SocketProxy::handleDownstreamHost1Connection);
m_downstreamHost2 = new QTcpServer(this);
m_downstreamHost2->listen(QHostAddress::LocalHost, 1235);
connect(m_downstreamHost2, &QTcpServer::newConnection, this, &SocketProxy::handleDownstreamHost2Connection);
// 创建两个上位机连接
m_upstreamHost1 = new QTcpSocket(this);
connect(m_upstreamHost1, &QTcpSocket::readyRead, this, &SocketProxy::handleUpstreamHost1Data);
connect(m_upstreamHost1, &QTcpSocket::disconnected, this, &SocketProxy::handleUpstreamHost1Disconnection);
m_upstreamHost2 = new QTcpSocket(this);
connect(m_upstreamHost2, &QTcpSocket::readyRead, this, &SocketProxy::handleUpstreamHost2Data);
connect(m_upstreamHost2, &QTcpSocket::disconnected, this, &SocketProxy::handleUpstreamHost2Disconnection);
}
private slots:
void handleDownstreamHost1Connection()
{
// 当下位机1连接时,创建一个上位机1连接到下位机1
QTcpSocket *downstreamSocket = m_downstreamHost1->nextPendingConnection();
m_downstreamSockets[downstreamSocket] = 1;
m_upstreamHost1->connectToHost(QHostAddress::LocalHost, 1236);
if (!m_upstreamHost1->waitForConnected()) {
qDebug() << "Failed to connect to upstream host 1";
return;
}
m_upstreamSockets[m_upstreamHost1] = downstreamSocket;
}
void handleDownstreamHost2Connection()
{
// 当下位机2连接时,创建一个上位机2连接到下位机2
QTcpSocket *downstreamSocket = m_downstreamHost2->nextPendingConnection();
m_downstreamSockets[downstreamSocket] = 2;
m_upstreamHost2->connectToHost(QHostAddress::LocalHost, 1237);
if (!m_upstreamHost2->waitForConnected()) {
qDebug() << "Failed to connect to upstream host 2";
return;
}
m_upstreamSockets[m_upstreamHost2] = downstreamSocket;
}
void handleUpstreamHost1Data()
{
// 将上位机1的数据转发到下位机1
QTcpSocket *upstreamSocket = qobject_cast<QTcpSocket*>(sender());
if (!upstreamSocket)
return;
QTcpSocket *downstreamSocket = m_upstreamSockets.value(upstreamSocket);
if (!downstreamSocket)
return;
QByteArray data = upstreamSocket->readAll();
downstreamSocket->write(data);
}
void handleUpstreamHost2Data()
{
// 将上位机2的数据转发到下位机2
QTcpSocket *upstreamSocket = qobject_cast<QTcpSocket*>(sender());
if (!upstreamSocket)
return;
QTcpSocket *downstreamSocket = m_upstreamSockets.value(upstreamSocket);
if (!downstreamSocket)
return;
QByteArray data = upstreamSocket->readAll();
downstreamSocket->write(data);
}
void handleUpstreamHost1Disconnection()
{
// 断开上位机1连接时,同时断开下位机1连接
QTcpSocket *upstreamSocket = qobject_cast<QTcpSocket*>(sender());
if (!upstreamSocket)
return;
QTcpSocket *downstreamSocket = m_upstreamSockets.value(upstreamSocket);
if (!downstreamSocket)
return;
m_downstreamSockets.remove(downstreamSocket);
m_upstreamSockets.remove(upstreamSocket);
downstreamSocket->disconnectFromHost();
upstreamSocket->deleteLater();
}
void handleUpstreamHost2Disconnection()
{
// 断开上位机2连接时,同时断开下位机2连接
QTcpSocket *upstreamSocket = qobject_cast<QTcpSocket*>(sender());
if (!upstreamSocket)
return;
QTcpSocket *downstreamSocket = m_upstreamSockets.value(upstreamSocket);
if (!downstreamSocket)
return;
m_downstreamSockets.remove(downstreamSocket);
m_upstreamSockets.remove(upstreamSocket);
downstreamSocket->disconnectFromHost();
upstreamSocket->deleteLater();
}
private:
QTcpServer *m_downstreamHost1;
QTcpServer *m_downstreamHost2;
QTcpSocket *m_upstreamHost1;
QTcpSocket *m_upstreamHost2;
QMap<QTcpSocket*, QTcpSocket*> m_upstreamSockets;
QMap<QTcpSocket*, int> m_downstreamSockets;
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
SocketProxy socketProxy;
return app.exec();
}
#include "main.moc"
```
这个示例中,我们创建了两个下位机连接(m_downstreamHost1 和 m_downstreamHost2),分别在本地主机的端口 1234 和 1235 上监听。当下位机连接时,我们创建一个代表上位机连接的 QTcpSocket,连接到本地主机的端口 1236 和 1237,这些端口上的服务将会与下位机连接。
我们在两个上位机连接(m_upstreamHost1 和 m_upstreamHost2)上监听数据和断开事件。当上位机发来数据时,我们将数据转发到相应的下位机连接中。
当上位机连接断开时,我们同时关闭相应的下位机连接,并从 m_upstreamSockets 和 m_downstreamSockets 映射中删除相应的条目。
请注意,这只是一个示例,你需要根据你的实际需求进行调整。例如,你可能需要添加一些错误处理代码来处理连接错误,或者添加一些界面元素来显示连接状态。
阅读全文