In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to realize the Qt network transit server". In the operation of the actual case, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
I. Preface
Demand scenarios:
The mobile phone or other end can control the device, check the various running status of the equipment, receive alarm push and so on.
At the same time, it supports local area network, wide area network and Internet access, especially Internet access.
Permission control, a given account controls the authorized device, and automatically pulls the device information.
Feedback tips should be given when the device is not online for analysis.
Each connection has its own unique number as an identifier.
It can be easily expanded to Wechat access + Mini Program access + web access.
2. Code idea # 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 () write (buffer); emit sendData (ip, port, deviceID, data);} TcpServer1::TcpServer1 (QObject * parent): QTcpServer (parent) {} # if (QT_VERSION > QT_VERSION_CHECK) 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); QString (QString, QString (QString, QString, QString)), connect (connect, client (connect, connect), (,) 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, "client online"); / / append to the linked list 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, "client offline"); / / remove clients.removeOne (client) from the linked list after disconnection } bool TcpServer1::start () {# if (QT_VERSION > QT_VERSION_CHECK) 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;} III.
This is the end of the content of "how to realize the Qt network transit server". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.