日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Qt中TCP服务端编程

發布時間:2025/4/5 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Qt中TCP服务端编程 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

    • 1 Qt中的TCP服務端編程
      • 1.1 TCP服務端編程介紹
      • 1.2 Qt中的TCP服務端編程

1 Qt中的TCP服務端編程

1.1 TCP服務端編程介紹

網絡中的服務端:

  • 服務端是為客戶端服務的,服務的內容諸如向客戶端提供資源,保存客戶端數據,為客戶端提供功能接口等。

Client/Server軟件架構簡介:

Client/Server軟件架構的特點:

  • 服務端被動接受連接(服務端無法主動連接客戶端)。
  • 服務端必須公開網絡地址(容易受到攻擊)。
  • 在職責上:
    • 客戶端傾向于處理用戶交互及體驗(GUI)。
    • 服務端傾向于用戶數據的組織和存儲(數據處理)。

Browser/Server軟件架構簡介(B/S):

  • B/S是一種特殊的C/S網絡架構。
  • B/S中的客戶端統一使用瀏覽器(Browser)。
  • B/S中的客戶端GUI通常采用HTML進行開發。
  • B/S中的客戶端與服務端通常采用http協議進行通信。

1.2 Qt中的TCP服務端編程


Qt中的TCP服務端編程:

  • Qt提供了QTcpServer類(封裝了TCP協議細節)。
  • 將QTcpServer的對象當作黑盒使用,進行連接監聽。
  • 每一個連接生成一個QTcpSocket對象進行通信。

QTcpServer的使用方式:

  • 禁停本機地址的端口(listen())。
  • 通過信號通知客戶端連接(newConnection())。
  • 獲取QTcpSocket通信對象(nextPendingConnection())。
  • 停止監聽(close())。
  • QTcpServer的注意事項:

    • 用于處理客戶端連接,不進行具體通信。
    • 監聽的端口只用于響應連接請求。
    • 監聽到連接后,生成QTcpSocket對象與客戶端通信。

    Client/Server交互流程:

    代碼實現如下:

    ServerDemo.h:

    #ifndef SERVERDEMO_H #define SERVERDEMO_H#include <QObject> #include <QTcpServer>class ServerDemo : public QObject {Q_OBJECTQTcpServer m_server;public:ServerDemo(QObject* parent = NULL);bool start(int port);void stop();~ServerDemo();protected slots:void onNewConnection();void onConnected();void onDisconnected();void onDataReady();void onBytesWritten(qint64 bytes); };#endif // SERVERDEMO_H

    ServerDemo.cpp:

    #include "ServerDemo.h" #include <QHostAddress> #include <QTcpSocket> #include <QObjectList> #include <QDebug>ServerDemo::ServerDemo(QObject* parent) : QObject(parent) {connect(&m_server, SIGNAL(newConnection()), this, SLOT(onNewConnection())); }void ServerDemo::onNewConnection() {qDebug() << "onNewConnection";QTcpSocket* tcp = m_server.nextPendingConnection();connect(tcp, SIGNAL(connected()), this, SLOT(onConnected()));connect(tcp, SIGNAL(disconnected()), this, SLOT(onDisconnected()));connect(tcp, SIGNAL(readyRead()), this, SLOT(onDataReady()));connect(tcp, SIGNAL(bytesWritten(qint64)), this, SLOT(onBytesWritten(qint64))); }void ServerDemo::onConnected() {QTcpSocket* tcp = dynamic_cast<QTcpSocket*>(sender());if( tcp != NULL ){qDebug() << "onConnected";qDebug() << "Local Address:" << tcp->localAddress();qDebug() << "Local Port:" << tcp->localPort();} }void ServerDemo::onDisconnected() {qDebug() << "onDisconnected"; }void ServerDemo::onDataReady() {QTcpSocket* tcp = dynamic_cast<QTcpSocket*>(sender());char buf[256] = {0};if( tcp != NULL ){qDebug() << "onDataReady:" << tcp->read(buf, sizeof(buf)-1);qDebug() << "Data:" << buf;} }void ServerDemo::onBytesWritten(qint64 bytes) {qDebug() << "onBytesWritten:" << bytes; }bool ServerDemo::start(int port) {bool ret = true;if( !m_server.isListening() ){ret = m_server.listen(QHostAddress("127.0.0.1"), port);}return ret; }void ServerDemo::stop() {if( m_server.isListening() ){m_server.close();} }ServerDemo::~ServerDemo() {const QObjectList& list = m_server.children();for(int i=0; i<list.length(); i++){QTcpSocket* tcp = dynamic_cast<QTcpSocket*>(list[i]);if( tcp != NULL ){tcp->close();}} }

    main.cpp:

    #include <QCoreApplication> #include <QTcpSocket> #include <QDebug> #include <QThread> #include "ClientDemo.h" #include "ServerDemo.h"void SyncClientDemo() {QTcpSocket client;char buf[256] = {0};client.connectToHost("127.0.0.1", 8080);qDebug() << "Connected:" << client.waitForConnected();qDebug() << "Send Bytes:" << client.write("D.T.Software");qDebug() << "Send Status:" << client.waitForBytesWritten();qDebug() << "Data Available:" << client.waitForReadyRead();qDebug() << "Received Bytes:" << client.read(buf, sizeof(buf)-1);qDebug() << "Received Data:" << buf;client.close();// client.waitForDisconnected(); }int main(int argc, char *argv[]) {QCoreApplication a(argc, argv);ServerDemo server;server.start(8080);return a.exec(); }

    參考資料:

  • QT實驗分析教程
  • 總結

    以上是生活随笔為你收集整理的Qt中TCP服务端编程的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。