1, Foreword
Qt has been developed for 10 years, including many projects. Based on the rapid development of web and mobile Internet, a large number of application scenarios need a network transit server, which can realize mobile app or other client remote control devices. Now the Internet of things is developing very rapidly, which is the general trend in the future, so I have this idea for a long time, and I plan to do it with Qt A simple network transfer server.
Demand scenario:
- The mobile terminal or other terminals can control the device back, check various operation states of the device, receive alarm push, etc.
- At the same time, it supports LAN, Wan, Internet access, especially Internet access.
- Authority control: give the account to control the authorized equipment, and automatically pull the equipment information.
- When the equipment is not online, feedback information prompt shall be given for analysis.
- Each connection has its own unique number as an identifier.
- It can be easily expanded to WeChat access + applet access +web access.
2, Code thinking
#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; } //Take out the unique identifier and filter it. You can change the filter conditions by yourself QByteArray cmd = data.mid(App::CmdStart1, App::CmdLen1); QString id = QString(cmd); if (id.startsWith("S") && deviceID != id) { deviceID = id; //Send signal update identifier 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, "Client online"); //Append to 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 from list after disconnection 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; }
3, Renderings
4, Open source Homepage
All of the above works are downloaded from the open source homepage, and the quantity and quality of works will be continuously updated. Welcome to pay attention.
- Domestic site: https://gitee.com/feiyangqingyun/QWidgetDemo
- International site: https://github.com/feiyangqingyun/QWidgetDemo
- Personal homepage: https://blog.csdn.net/feiyangqingyun
- Zhihu homepage: https://www.zhihu.com/people/feiyangqingyun/