用qt写一段socket代理层的代码实现两个上位机同时连接两个下位机,并将两个下位机的数据呈现在上位机上
时间: 2024-03-15 17:45:33 浏览: 23
以下是使用QT编写的Socket代理层的代码,可以实现两个上位机同时连接两个下位机,并将两个下位机的数据呈现在上位机上。请注意,该代码仅供参考,需要根据具体情况进行调整和修改。
```cpp
#include <QTcpServer>
#include <QTcpSocket>
class SocketProxy : public QObject
{
Q_OBJECT
public:
explicit SocketProxy(QObject *parent = nullptr);
signals:
public slots:
void onClient1Connected();
void onClient1ReadyRead();
void onClient1Disconnected();
void onClient2Connected();
void onClient2ReadyRead();
void onClient2Disconnected();
void onDownstream1Connected();
void onDownstream1ReadyRead();
void onDownstream1Disconnected();
void onDownstream2Connected();
void onDownstream2ReadyRead();
void onDownstream2Disconnected();
private:
QTcpServer *m_server1;
QTcpServer *m_server2;
QTcpSocket *m_client1;
QTcpSocket *m_client2;
QTcpSocket *m_downstream1;
QTcpSocket *m_downstream2;
};
SocketProxy::SocketProxy(QObject *parent) : QObject(parent)
{
m_server1 = new QTcpServer(this);
m_server2 = new QTcpServer(this);
connect(m_server1, &QTcpServer::newConnection, this, &SocketProxy::onClient1Connected);
connect(m_server2, &QTcpServer::newConnection, this, &SocketProxy::onClient2Connected);
if (!m_server1->listen(QHostAddress::Any, 8888)) {
qDebug() << "Failed to start server1: " << m_server1->errorString();
return;
}
if (!m_server2->listen(QHostAddress::Any, 9999)) {
qDebug() << "Failed to start server2: " << m_server2->errorString();
return;
}
}
void SocketProxy::onClient1Connected()
{
m_client1 = m_server1->nextPendingConnection();
connect(m_client1, &QTcpSocket::readyRead, this, &SocketProxy::onClient1ReadyRead);
connect(m_client1, &QTcpSocket::disconnected, this, &SocketProxy::onClient1Disconnected);
qDebug() << "Client1 connected";
}
void SocketProxy::onClient1ReadyRead()
{
if (!m_downstream1) {
m_downstream1 = new QTcpSocket(this);
connect(m_downstream1, &QTcpSocket::connected, this, &SocketProxy::onDownstream1Connected);
connect(m_downstream1, &QTcpSocket::readyRead, this, &SocketProxy::onDownstream1ReadyRead);
connect(m_downstream1, &QTcpSocket::disconnected, this, &SocketProxy::onDownstream1Disconnected);
m_downstream1->connectToHost(QHostAddress("192.168.1.1"), 8888);
}
m_downstream1->write(m_client1->readAll());
}
void SocketProxy::onClient1Disconnected()
{
m_client1->deleteLater();
m_downstream1->deleteLater();
m_client1 = nullptr;
m_downstream1 = nullptr;
qDebug() << "Client1 disconnected";
}
void SocketProxy::onClient2Connected()
{
m_client2 = m_server2->nextPendingConnection();
connect(m_client2, &QTcpSocket::readyRead, this, &SocketProxy::onClient2ReadyRead);
connect(m_client2, &QTcpSocket::disconnected, this, &SocketProxy::onClient2Disconnected);
qDebug() << "Client2 connected";
}
void SocketProxy::onClient2ReadyRead()
{
if (!m_downstream2) {
m_downstream2 = new QTcpSocket(this);
connect(m_downstream2, &QTcpSocket::connected, this, &SocketProxy::onDownstream2Connected);
connect(m_downstream2, &QTcpSocket::readyRead, this, &SocketProxy::onDownstream2ReadyRead);
connect(m_downstream2, &QTcpSocket::disconnected, this, &SocketProxy::onDownstream2Disconnected);
m_downstream2->connectToHost(QHostAddress("192.168.1.2"), 9999);
}
m_downstream2->write(m_client2->readAll());
}
void SocketProxy::onClient2Disconnected()
{
m_client2->deleteLater();
m_downstream2->deleteLater();
m_client2 = nullptr;
m_downstream2 = nullptr;
qDebug() << "Client2 disconnected";
}
void SocketProxy::onDownstream1Connected()
{
qDebug() << "Downstream1 connected";
}
void SocketProxy::onDownstream1ReadyRead()
{
if (m_client1) {
m_client1->write(m_downstream1->readAll());
}
}
void SocketProxy::onDownstream1Disconnected()
{
qDebug() << "Downstream1 disconnected";
}
void SocketProxy::onDownstream2Connected()
{
qDebug() << "Downstream2 connected";
}
void SocketProxy::onDownstream2ReadyRead()
{
if (m_client2) {
m_client2->write(m_downstream2->readAll());
}
}
void SocketProxy::onDownstream2Disconnected()
{
qDebug() << "Downstream2 disconnected";
}
```
这段代码实现了一个简单的Socket代理,它监听两个端口(8888和9999),并将来自两个上位机的连接分别转发到两个下位机上。在收到来自下位机的数据时,它会将数据发送给对应的上位机。你可以根据具体情况进行修改,比如更改IP地址和端口号,或者添加更多的上下位机连接。
阅读全文