您好,登錄后才能下訂單哦!
本篇內容介紹了“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; }
“Qt網絡中轉服務器怎么實現”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。