欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Qt網(wǎng)絡中轉(zhuǎn)服務器怎么實現(xiàn)

本篇內(nèi)容介紹了“Qt網(wǎng)絡中轉(zhuǎn)服務器怎么實現(xiàn)”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!

創(chuàng)新互聯(lián)建站專業(yè)為企業(yè)提供志丹網(wǎng)站建設、志丹做網(wǎng)站、志丹網(wǎng)站設計、志丹網(wǎng)站制作等企業(yè)網(wǎng)站建設、網(wǎng)頁設計與制作、志丹企業(yè)網(wǎng)站模板建站服務,十多年志丹做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡服務。

一、前言

需求場景:

  1. 手機端或者其他端可以對設備進行回控,并查看設備各種運行狀態(tài),接收報警推送等。

  2. 同時支持在局域網(wǎng)、廣域網(wǎng)、互聯(lián)網(wǎng)訪問,尤其是互聯(lián)網(wǎng)訪問。

  3. 權限控制,給定賬號控制授權的設備,并自動拉取設備信息。

  4. 設備不在線要給出反饋信息提示以便分析。

  5. 每個連接都有自己的唯一編號作為標識符。

  6. 可以方便的拓展為微信接入+小程序接入+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;
        //發(fā)送信號更新標識符
        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網(wǎng)絡中轉(zhuǎn)服務器怎么實現(xiàn)

“Qt網(wǎng)絡中轉(zhuǎn)服務器怎么實現(xiàn)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關的知識可以關注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!

標題名稱:Qt網(wǎng)絡中轉(zhuǎn)服務器怎么實現(xiàn)
分享URL:http://www.chinadenli.net/article44/iphoee.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供App開發(fā)移動網(wǎng)站建設定制網(wǎng)站云服務器做網(wǎng)站靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化