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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Qt中TCP服务端编程

發(fā)布時(shí)間:2025/4/5 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Qt中TCP服务端编程 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

    • 1 Qt中的TCP服務(wù)端編程
      • 1.1 TCP服務(wù)端編程介紹
      • 1.2 Qt中的TCP服務(wù)端編程

1 Qt中的TCP服務(wù)端編程

1.1 TCP服務(wù)端編程介紹

網(wǎng)絡(luò)中的服務(wù)端:

  • 服務(wù)端是為客戶(hù)端服務(wù)的,服務(wù)的內(nèi)容諸如向客戶(hù)端提供資源,保存客戶(hù)端數(shù)據(jù),為客戶(hù)端提供功能接口等。

Client/Server軟件架構(gòu)簡(jiǎn)介:

Client/Server軟件架構(gòu)的特點(diǎn):

  • 服務(wù)端被動(dòng)接受連接(服務(wù)端無(wú)法主動(dòng)連接客戶(hù)端)。
  • 服務(wù)端必須公開(kāi)網(wǎng)絡(luò)地址(容易受到攻擊)。
  • 在職責(zé)上:
    • 客戶(hù)端傾向于處理用戶(hù)交互及體驗(yàn)(GUI)。
    • 服務(wù)端傾向于用戶(hù)數(shù)據(jù)的組織和存儲(chǔ)(數(shù)據(jù)處理)。

Browser/Server軟件架構(gòu)簡(jiǎn)介(B/S):

  • B/S是一種特殊的C/S網(wǎng)絡(luò)架構(gòu)。
  • B/S中的客戶(hù)端統(tǒng)一使用瀏覽器(Browser)。
  • B/S中的客戶(hù)端GUI通常采用HTML進(jìn)行開(kāi)發(fā)。
  • B/S中的客戶(hù)端與服務(wù)端通常采用http協(xié)議進(jìn)行通信。

1.2 Qt中的TCP服務(wù)端編程


Qt中的TCP服務(wù)端編程:

  • Qt提供了QTcpServer類(lèi)(封裝了TCP協(xié)議細(xì)節(jié))。
  • 將QTcpServer的對(duì)象當(dāng)作黑盒使用,進(jìn)行連接監(jiān)聽(tīng)。
  • 每一個(gè)連接生成一個(gè)QTcpSocket對(duì)象進(jìn)行通信。

QTcpServer的使用方式:

  • 禁停本機(jī)地址的端口(listen())。
  • 通過(guò)信號(hào)通知客戶(hù)端連接(newConnection())。
  • 獲取QTcpSocket通信對(duì)象(nextPendingConnection())。
  • 停止監(jiān)聽(tīng)(close())。
  • QTcpServer的注意事項(xiàng):

    • 用于處理客戶(hù)端連接,不進(jìn)行具體通信。
    • 監(jiān)聽(tīng)的端口只用于響應(yīng)連接請(qǐng)求。
    • 監(jiān)聽(tīng)到連接后,生成QTcpSocket對(duì)象與客戶(hù)端通信。

    Client/Server交互流程:

    代碼實(shí)現(xiàn)如下:

    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實(shí)驗(yàn)分析教程
  • 總結(jié)

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

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。