Qt開源作品41-網絡調試助手增強版V2022


一、前言

做網絡通信少不了網絡收發數據,經常用到網絡數據的調試相關工具,以便偵聽數據用來判斷數據是否正確,許久以前就發布過類似的工具,第一版大概在2013年,第二版大概在2017年,中間參考過不少的網絡調試助手,也有些叫網絡調試工具等等,個人覺得做得最好的還是野人家園的NetAssist,小巧綠色,功能強大。

中間不少網友提過很多建議,比如為何沒有Udp客戶端只有Udp服務器,其實Udp通信是無連接的,意味着QUdpSocket即是客戶端也是服務器,但是根據眾多用戶的操作習慣以及編程對稱性法則,還是單獨又做了個Udp客戶端。如今WebSocket也非常流行,客戶端工具和網頁之間通信可以直接用上socket之類的機制,而且自從Qt5以后有了WebSocket模塊,使用非常簡單,封裝的QWebSocket、QWebSocketServer(很奇怪這里沒有叫QWebServer?)和QTcpSocket、QTcpServer、QUdpSocket用法幾乎一致。

二、主要功能

  1. Tcp客戶端模塊。
  2. Tcp服務器模塊。
  3. Udp客戶端模塊。
  4. Udp服務器模塊。
  5. WebSocket客戶端模塊。
  6. WebSocket服務器模塊。
  7. 服務器支持多個客戶端連接。
  8. Ascii字符數據收發。
  9. Hex16進制數據收發。
  10. 支持Utf8中文數據收發。
  11. 可指定網卡IP地址綁定。
  12. 可暫停顯示收發數據。
  13. 定時器自動發送。
  14. 可對單個在線連接發送數據,也可勾選全部連接進行發送。
  15. 可配置常用發送數據(send.txt),自動從配置文件加載數據發送下拉框的數據。
  16. 可啟用設備模擬回復(device.txt),當收到某個數據時,模擬設備自動應答回復數據。
  17. 自動從配置文件加載最后的界面設置。
  18. 同時支持Qt4、Qt5、Qt6。
  19. 同時支持win、linux、mac、嵌入式linux、樹莓派等。
  20. 每個模塊功能都是獨立的一個Form,可以很方便的直接new,這樣需要多少個就new多少個,用戶可以任意指定動態新建多個客戶端和服務器。

三、效果圖

四、開源主頁

  • 以上作品完整源碼下載都在開源主頁,會持續不斷更新作品數量和質量,歡迎各位關注。
  • 本開源項目已經成功升級到V2.0版本,分門別類,圖文並茂,保你爽到爆。
  • Qt開源武林秘籍開發經驗,看完學完,20K起薪,沒有找我!
  1. 國內站點:https://gitee.com/feiyangqingyun/QWidgetDemo
  2. 國際站點:https://github.com/feiyangqingyun/QWidgetDemo
  3. 開源秘籍:https://gitee.com/feiyangqingyun/qtkaifajingyan
  4. 個人主頁:https://qtchina.blog.csdn.net/
  5. 知乎主頁:https://www.zhihu.com/people/feiyangqingyun/

五、核心代碼

//第一步:實例化對應的類
tcpSocket = new QTcpSocket(this);
connect(tcpSocket, SIGNAL(connected()), this, SLOT(connected()));
connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(disconnected()));
connect(tcpSocket, SIGNAL(disconnected()), this, SLOT(disconnected()));
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readData()));

tcpServer = new TcpServer(this);
connect(tcpServer, SIGNAL(clientConnected(QString, int)), this, SLOT(clientConnected(QString, int)));
connect(tcpServer, SIGNAL(clientDisconnected(QString, int)), this, SLOT(clientDisconnected(QString, int)));
connect(tcpServer, SIGNAL(sendData(QString, int, QString)), this, SLOT(sendData(QString, int, QString)));
connect(tcpServer, SIGNAL(receiveData(QString, int, QString)), this, SLOT(receiveData(QString, int, QString)));

udpSocket = new QUdpSocket(this);
connect(udpSocket, SIGNAL(readyRead()), this, SLOT(readData()));

//第二步:收發數據
void frmTcpClient::readData()
{
    QByteArray data = tcpSocket->readAll();
    if (data.length() <= 0) {
        return;
    }

    QString buffer;
    if (App::HexReceiveTcpClient) {
        buffer = QUIHelper::byteArrayToHexStr(data);
    } else if (App::AsciiTcpClient) {
        buffer = QUIHelper::byteArrayToAsciiStr(data);
    } else {
        buffer = QString(data);
    }

    append(1, buffer);

    //自動回復數據,可以回復的數據是以;隔開,每行可以帶多個;所以這里不需要繼續判斷
    if (App::DebugTcpClient) {
        int count = App::Keys.count();
        for (int i = 0; i < count; i++) {
            if (App::Keys.at(i) == buffer) {
                sendData(App::Values.at(i));
                break;
            }
        }
    }
}

void frmUdpClient::readData()
{
    QHostAddress host;
    quint16 port;
    QByteArray data;
    QString buffer;

    while (udpSocket->hasPendingDatagrams()) {
        data.resize(udpSocket->pendingDatagramSize());
        udpSocket->readDatagram(data.data(), data.size(), &host, &port);

        if (App::HexReceiveUdpClient) {
            buffer = QUIHelper::byteArrayToHexStr(data);
        } else if (App::AsciiUdpClient) {
            buffer = QUIHelper::byteArrayToAsciiStr(data);
        } else {
            buffer = QString(data);
        }

        QString ip = host.toString();
        ip = ip.replace("::ffff:", "");
        if (ip.isEmpty()) {
            continue;
        }

        QString str = QString("[%1:%2] %3").arg(ip).arg(port).arg(buffer);
        append(1, str);

        if (App::DebugUdpClient) {
            int count = App::Keys.count();
            for (int i = 0; i < count; i++) {
                if (App::Keys.at(i) == buffer) {
                    sendData(ip, port, App::Values.at(i));
                    break;
                }
            }
        }
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM