用qt写一段socket代理的代码实现两个上位机同时连接两个下位机,并通过上位机下发命令
时间: 2024-03-13 17:42:28 浏览: 131
好的,我可以为你提供一个基于Qt的socket代理代码,实现两个上位机同时连接两个下位机,并通过上位机下发命令的功能。代码如下:
```cpp
#include <QtCore/QCoreApplication>
#include <QtCore/QDebug>
#include <QtCore/QCommandLineParser>
#include <QtNetwork/QTcpServer>
#include <QtNetwork/QTcpSocket>
class SocketProxy : public QObject
{
Q_OBJECT
public:
explicit SocketProxy(quint16 localPort, quint16 remotePort, QObject *parent = nullptr)
: QObject(parent)
, m_localPort(localPort)
, m_remotePort(remotePort)
{
m_server = new QTcpServer(this);
connect(m_server, &QTcpServer::newConnection, this, &SocketProxy::onNewConnection);
if (!m_server->listen(QHostAddress::Any, m_localPort)) {
qCritical() << "Failed to listen on port" << m_localPort;
QCoreApplication::exit(1);
return;
}
qDebug() << "Listening on port" << m_localPort << "forwarding to port" << m_remotePort;
}
private slots:
void onNewConnection()
{
QTcpSocket *clientSocket = m_server->nextPendingConnection();
connect(clientSocket, &QTcpSocket::readyRead, this, &SocketProxy::onReadyRead);
connect(clientSocket, &QTcpSocket::disconnected, clientSocket, &QTcpSocket::deleteLater);
QTcpSocket *serverSocket = new QTcpSocket(this);
connect(serverSocket, &QTcpSocket::connected, this, &SocketProxy::onConnected);
connect(serverSocket, &QTcpSocket::readyRead, this, &SocketProxy::onReadyRead);
connect(serverSocket, &QTcpSocket::disconnected, serverSocket, &QTcpSocket::deleteLater);
serverSocket->connectToHost(QHostAddress::LocalHost, m_remotePort);
m_clientToServer.insert(clientSocket, serverSocket);
m_serverToClient.insert(serverSocket, clientSocket);
}
void onConnected()
{
QTcpSocket *serverSocket = qobject_cast<QTcpSocket *>(sender());
QTcpSocket *clientSocket = m_serverToClient.value(serverSocket, nullptr);
if (clientSocket) {
qDebug() << "Connection established between" << clientSocket->peerAddress().toString() << ":" << clientSocket->peerPort() << "and localhost:" << m_remotePort;
}
}
void onReadyRead()
{
QTcpSocket *socket = qobject_cast<QTcpSocket *>(sender());
if (m_clientToServer.contains(socket)) {
QTcpSocket *serverSocket = m_clientToServer[socket];
serverSocket->write(socket->readAll());
} else if (m_serverToClient.contains(socket)) {
QTcpSocket *clientSocket = m_serverToClient[socket];
clientSocket->write(socket->readAll());
}
}
private:
quint16 m_localPort;
quint16 m_remotePort;
QTcpServer *m_server;
QHash<QTcpSocket *, QTcpSocket *> m_clientToServer;
QHash<QTcpSocket *, QTcpSocket *> m_serverToClient;
};
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
QCoreApplication::setApplicationName("SocketProxy");
QCoreApplication::setApplicationVersion("1.0");
QCommandLineParser parser;
parser.setApplicationDescription("A simple socket proxy");
parser.addHelpOption();
parser.addVersionOption();
parser.addOption({{"l", "local"}, "Local listening port", "port", "1234"});
parser.addOption({{"r", "remote"}, "Remote target port", "port", "5678"});
parser.process(app);
quint16 localPort = parser.value("local").toUShort();
quint16 remotePort = parser.value("remote").toUShort();
SocketProxy proxy(localPort, remotePort);
return app.exec();
}
#include "main.moc"
```
这个代码实例化了一个 SocketProxy 类,它监听本地的 localPort 端口,并将所有连接转发到远程的 remotePort 端口。然后,每当有新的连接时,它会在客户端和服务器之间建立两个QTcpSocket对象,并将其用于转发数据。
阅读全文