目錄Qt 學習筆記全系列傳送門:
Qt 學習筆記 - 第一章 - 快速開始、信號與槽
Qt 學習筆記 - 第二章 - 添加圖片、布局、界面切換
Qt 學習筆記 - 第三章 - Qt的三駕馬車之一 - 串口編程 + 程式打包成Windows軟體
【本章】Qt 學習筆記 - 第四章 - Qt的三駕馬車之二 - 網路編程
Qt 學習筆記 - 第五章 - Qt 時間編程 - Qt 時鐘
- 1、TCP 通信
- 1.1 TCP 編程的特點
- 1.2 TCP 服務器案例
- 1.3 TCP 客戶端案例
- 2、UDP 通信
- 1.1 UDP 編程的特點
- 1.2 UDP 客戶端
1、TCP 通信
1.1 TCP 編程的特點
- 包含服務器和客戶端
- 使用時需要在工程檔案中引入
QT += network并在使用時匯入包 - 需要使用到的類有
- QTcpServer
- QTcpSocket
1.2 TCP 服務器案例
需要先定義并在構造中初始化
QTcpServer *tcpServer和QTcpSocket *tcpSocket:// 這里傳入 this 時,父物件被洗掉時子物件也會被洗掉,省去了delete的麻煩 tcpServer = new QTcpServer(this); tcpSocket = new QTcpSocket(this);
-
工程檔案
QT += core gui network -
UI
-
接收框,只讀
-
埠號輸入框和提示文字
-
發送視窗
-
按鈕,打開服務器、關閉服務器、發送
-
控制元件改名
-
-
邏輯功能
-
監聽:槽函式,點擊打開服務器時,需要監聽對應埠是否被訪問
Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); // 新建tcpServer和tcpSocket的物件,成員屬性在頭檔案中已定義,細節不表 tcpServer = new QTcpServer(this); tcpSocket = new QTcpSocket(this); } void Widget::on_openBt_clicked() { // 開啟監聽,監聽所有人的連接,引數指定所有人和一個無符號整型埠 tcpServer->listen(QHostAddress::Any, ui->portEdit->text().toUInt()); } -
接收:包含獲取和展示兩個部分
-
獲取
Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); tcpServer = new QTcpServer(this); tcpSocket = new QTcpSocket(this); // 當監測到新的連接產生的信號時,呼叫槽函式newConnection_Slot() connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newConnection_Slot())); } void Widget::newConnection_Slot() { // 獲取到已經連接的客戶端的Socket tcpSocket = tcpServer->nextPendingConnection(); // 當存在可讀資料時,呼叫槽函式readyRead_Slot(),用于展示到接收框中 connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readyRead_Slot())); } -
展示到接收框
void Widget::readyRead_Slot() { // 讀取tcpSocket中的內容,使用字串接收 QString buf = tcpSocket->readAll(); // 將讀取到的資訊展示到接收框中 ui->recvEdit->appendPlainText(buf); }
-
-
發送
void Widget::on_sendBt_clicked() { // 需要轉成 char* tcpSocket->write(ui->sendEdit->text().toLocal8Bit().data()); } -
關閉
void Widget::on_closeBt_clicked() { tcpServer->close(); tcpSocket->close(); }
-
1.3 TCP 客戶端案例
-
工程檔案
QT += core gui network -
UI
-
接收框,只讀
-
埠號輸入框和提示文字、IP地址輸入框和提示文字
-
發送視窗
-
按鈕,打開服務器、關閉服務器、發送
-
控制元件改名
-
-
邏輯功能
需要先定義并在構造中初始化
QTcpSocket *tcpSocket:?
tcpSocket = new QTcpSocket(this);-
連接
void Widget::on_openBt_clicked() { // 點擊打開客戶端時,應連接服務器,取界面上的ip和埠,埠要轉Uint tcpSocket->connectToHost(ui->ipEdit->text(), ui->portEdit->text().toUInt()); // 當監聽到連接成功的信號connected()時,呼叫槽函式connected_Slot() connect(tcpSocket, SIGNAL(connected()), this, SLOT(connected_Slot())); } -
接收
void Widget::connected_Slot() { // 當監聽到有東西可讀的信號readyRead()時,呼叫槽函式 connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readyRead_Slot())); } void Widget::readyRead_Slot() { // 讀取tcpSocket中的資訊,展示到頁面上 ui->recvEdit->appendPlainText(tcpSocket->readAll()); } -
發送
void Widget::on_sendBt_clicked() { // 發送時將發送框中的內容轉為 char* 寫入tcpSocket tcpSocket->write(ui->sendEdit->text().toLocal8Bit()); } -
關閉
void Widget::on_closeBt_clicked() { tcpSocket->close(); }
-
2、UDP 通信
1.1 UDP 編程的特點
-
不分客戶端和服務器
-
使用時需要在工程檔案中引入
QT += network并在使用時匯入包 -
需要使用 QUdpSocket 類
1.2 UDP 客戶端
需要先定義并在構造中初始化
QUdpSocket *udpSocket:?
udpSocket = new QUdpSocket(this);
-
工程檔案
QT += core gui network -
UI
- 接收框,只讀
- 埠號輸入框和提示文字、目標埠號輸入框和提示文字、IP地址輸入框和提示文字
- 發送視窗
- 按鈕,打開服務器、關閉服務器、發送
- 控制元件改名
-
邏輯功能
-
啟動
void Widget::on_openBt_clicked() { // 將埠號系結到Socket if (udpSocket->bind(ui->localPortEdit->text().toUInt())) { QMessageBox::information(this, "提示", "成功"); } else { QMessageBox::information(this, "提示", "失敗"); } // 關聯readyRead信號,有可讀信號呼叫槽函式 connect(udpSocket, SIGNAL(readyRead()), this, SLOT(readyRead_slot())); } -
接收
void Widget::readyRead_slot() { // hasPendingDatagrams()用于判斷是否讀取完,若沒有讀完則回傳true while (udpSocket->hasPendingDatagrams()) { QByteArray arr; // 將陣列調整為和 udpSocket 中剩下的資料大小一樣 arr.resize(udpSocket->pendingDatagramSize()); // 將udpSocket中的資訊讀取到arr中,引數為接收陣列和接收的資料長度 udpSocket->readDatagram(arr.data(), arr.size()); // 將資訊寫到頁面上 QString buffer = arr.data(); ui->recvEdit->appendPlainText(buffer); } } -
發送
void Widget::on_sendBt_clicked() { qunit16 port = ui->aimPortEdit->text().toUInt(); QString sendbuffer = ui->sendEdit->text(); QHostAddress addr; addr.setAddress(ui->aimIpEdit->text()); // 發送的內容需要轉成 char* udpSocket->writeDatagram(sendbuffer.toLocal8Bit().data(), sendbuffer.length(), addr, port); } -
關閉
void Widget::on_closeBt_clicked() { udpSocket->close(); }
-
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/546959.html
標籤:其他
上一篇:Bokeh 環境設定
下一篇:Mysql基礎知識
