qt编写网络中转服务器开源,Qt开源作品37-网络中转服务器

本文介绍了一个使用Qt开发的简单网络中转服务器项目,旨在实现手机或其他客户端远程控制设备的功能。支持不同网络环境下访问,具备权限控制及设备状态反馈机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、前言

用Qt做开发10年了,其中做过好多项目,基于现在web和移动互联网发展如此迅猛,大量的应用场景需要一个网络中转服务器,可以实现手机app或者其他客户端远程回控设备,现在物联网发展非常迅猛,这个将来也是大势所趋,所以有这个想法很久了,打算用Qt也来做个简单的网络中转服务器。

需求场景:

手机端或者其他端可以对设备进行回控,并查看设备各种运行状态,接收报警推送等。

同时支持在局域网、广域网、互联网访问,尤其是互联网访问。

权限控制,给定账号控制授权的设备,并自动拉取设备信息。

设备不在线要给出反馈信息提示以便分析。

每个连接都有自己的唯一编号作为标识符。

可以方便的拓展为微信接入+小程序接入+web接入。

二、代码思路

#include "tcpserver1.h"

#include "quiwidget.h"

TcpClient1::TcpClient1(QObject *parent) : QTcpSocket(parent)

{

ip = "127.0.0.1";

port = 6907;

deviceID = "SSJC00000001";

connect(this, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(deleteLater()));

connect(this, SIGNAL(disconnected()), this, SLOT(deleteLater()));

connect(this, SIGNAL(readyRead()), this, SLOT(readData()));

}

void TcpClient1::setIP(const QString &ip)

{

this->ip = ip;

}

QString TcpClient1::getIP() const

{

return this->ip;

}

void TcpClient1::setPort(int port)

{

this->port = port;

}

int TcpClient1::getPort() const

{

return this->port;

}

QString TcpClient1::getDeviceID()

{

return this->deviceID;

}

void TcpClient1::readData()

{

QByteArray data = this->readAll();

if (data.length() <= 0) {

return;

}

//取出唯一标识符,并过滤,可自行更改过滤条件

QByteArray cmd = data.mid(App::CmdStart1, App::CmdLen1);

QString id = QString(cmd);

if (id.startsWith("S") && deviceID != id) {

deviceID = id;

//发送信号更新标识符

emit receiveDeviceID(ip, port, deviceID);

}

QString buffer;

if (App::HexData1) {

buffer = QUIHelper::byteArrayToHexStr(data);

} else {

buffer = QString(data);

}

emit receiveData(ip, port, deviceID, buffer);

}

void TcpClient1::sendData(const QString &data)

{

QByteArray buffer;

if (App::HexData1) {

buffer = QUIHelper::hexStrToByteArray(data);

} else {

buffer = data.toLatin1();

}

this->write(buffer);

emit sendData(ip, port, deviceID, data);

}

TcpServer1::TcpServer1(QObject *parent) : QTcpServer(parent)

{

}

#if (QT_VERSION > QT_VERSION_CHECK(5,0,0))

void TcpServer1::incomingConnection(qintptr handle)

#else

void TcpServer1::incomingConnection(int handle)

#endif

{

TcpClient1 *client = new TcpClient1(this);

client->setSocketDescriptor(handle);

connect(client, SIGNAL(disconnected()), this, SLOT(disconnected()));

connect(client, SIGNAL(sendData(QString, int, QString, QString)), this, SIGNAL(sendData(QString, int, QString, QString)));

connect(client, SIGNAL(receiveData(QString, int, QString, QString)), this, SIGNAL(receiveData(QString, int, QString, QString)));

connect(client, SIGNAL(receiveDeviceID(QString, int, QString)), this, SIGNAL(receiveDeviceID(QString, int, QString)));

QString ip = client->peerAddress().toString();

int port = client->peerPort();

QString deviceID = client->getDeviceID();

client->setIP(ip);

client->setPort(port);

emit clientConnected(ip, port, deviceID);

emit sendData(ip, port, deviceID, "客户端上线");

//追加到链表中

clients.append(client);

}

void TcpServer1::disconnected()

{

TcpClient1 *client = (TcpClient1 *)sender();

QString ip = client->getIP();

int port = client->getPort();

QString deviceID = client->getDeviceID();

emit clientDisconnected(ip, port, deviceID);

emit sendData(ip, port, deviceID, "客户端下线");

//断开连接后从链表中移除

clients.removeOne(client);

}

bool TcpServer1::start()

{

#if (QT_VERSION > QT_VERSION_CHECK(5,0,0))

bool ok = listen(QHostAddress::AnyIPv4, App::ListenPort1);

#else

bool ok = listen(QHostAddress::Any, App::ListenPort1);

#endif

return ok;

}

void TcpServer1::stop()

{

foreach (TcpClient1 *client, clients) {

client->disconnectFromHost();

}

this->close();

}

bool TcpServer1::writeData(const QString &deviceID, const QString &data)

{

bool ok = false;

foreach (TcpClient1 *client, clients) {

if (client->getDeviceID() == deviceID) {

client->sendData(data);

ok = true;

}

}

return ok;

}

三、效果图

2e17fa6a541e0792735f43b36d92d5f3.png

四、开源主页

以上作品完整源码下载都在开源主页,会持续不断更新作品数量和质量,欢迎各位关注。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值