Qt网络中转服务器怎么实现
发表于:2024-10-25 作者:千家信息网编辑
千家信息网最后更新 2024年10月25日,本篇内容介绍了"Qt网络中转服务器怎么实现"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!一、前言需求
千家信息网最后更新 2024年10月25日Qt网络中转服务器怎么实现
本篇内容介绍了"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)#elsevoid 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;}
三、效果图
"Qt网络中转服务器怎么实现"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!
设备
接入
服务器
网络
中转
服务
互联网
信息
内容
客户
客户端
更多
知识
互联
控制
实用
学有所成
接下来
下线
代码
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
服务器断电挂载磁盘错误
t3不能更改服务器连接
软件开发 西安 智联招聘
数据库中弱三范怎么理解
网络安全渗透审计检测平台
网络安全英语论文
电数宝数据库账号
计算机网络技术web是什么
网络安全员的事例
网络安全和国际合作专业委员会
请问服务器请求失败要怎么弄
软件开发能计入在建
延平租房软件开发
数据库某一条数据最近修改的时间
浙江九域网络技术
mui 跳转页面加数据库
英雄联盟每个区的服务器位置
儿童服务器多少钱一个
网络安全对于大学生的理解
属于国外全文数据库的有哪些
服务器部署静态网站
添加mt4服务器
网络安全法中关于言论的内容
消防通信网络技术
如何登陆本地服务器
sql数据库锁
数据库的数据结构
大学可以用的数学软件开发
南昌服务器散热片定制
儿童服务器多少钱一个