一、前言
用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;
}
三、效果圖
四、開源主頁
以上作品完整源碼下載都在開源主頁,會持續不斷更新作品數量和質量,歡迎各位關注。