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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

QT学习:基于UDP的网络广播程序

發布時間:2024/9/30 c/c++ 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 QT学习:基于UDP的网络广播程序 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

用戶數據報協議(User Data Protocol,UDP)是一種簡單輕量級、不可靠、面向數據報、無連接的 傳輸層協議,可以應用在可靠性不是十分重要的場合,如短消息、廣播信息等。 適合應用的情況有以下幾種:
(1)網絡數據大多為短消息。
(2) 擁有大量客戶端。
(3) 對數據安全性無特殊要求。
(4)網絡負擔非常重,但對響應速度要求高。

一、UDP工作原理

如下圖所示,UDP客戶端向UDP服務器發送一定長度的請求報文,報文大小的限制與各系統 的協議實現有關,但不得超過其下層IP規定的64KB;UDP服務器同樣以報文形式做出響應。如果服務器未收到此請求,客戶端不會進行重發,因此報文的傳輸是不可靠的。

二、UDP 編程模型

下面介紹基于UDP的經典編程模型,UDP客戶端與服務器間的交互時序如下圖所示。 可以看出,在UDP方式下,客戶端并不與服務器建立連接,它只負責調用發送函數向服務器發 出數據報。類似地,服務器也不從客戶端接收連接,只負責調用接收函數,等待來自某客戶端的數據到達。

三、UDP服務器編程實例

服務器端的編程具體代碼如下:
(1)在頭文件“udpserver.h”中聲明了需要的各種控件,其具體代碼如下:

#include <QDialog> #include <QLabel> #include <QLineEdit> #include <QPushButton> #include <QVBoxLayout> class UdpServer : public QDialog { Q_OBJECT public: UdpServer(QWidget *parent=0,Qt::WindowFlags f=0); ~UdpServer(); private: QLabel *TimerLabel; QLineEdit *TextLineEdit; QPushButton *StartBtn; QVBoxLayout *mainLayout; };

(2)源文件“udpserver.cpp”的具體代碼如下:

#include "udpserver.h" UdpServer::UdpServer(QWidget *parent,Qt::WindowFlags f) : QDialog(parent,f) { setWindowTitle(tr("UDP Server")); //設置窗體的標題 /* 初始化各個控件 */ TimerLabel = new QLabel(tr("計時器:"),this); TextLineEdit = new QLineEdit(this); StartBtn = new QPushButton(tr("開始"),this); /* 設置布局 */ mainLayout = new QVBoxLayout(this); mainLayout->addWidget(TimerLabel); mainLayout->addWidget(TextLineEdit); mainLayout->addWidget(StartBtn); }

(3)服務器界面如下圖所示:


具體操作步驟如下。
(1)在“UdpServer.pro”中添加如下語句:

QT += network

(2)在頭文件“udpserver.h”中添加需要的槽函數,其具體代碼如下:

#include <QUdpSocket> #include <QTimer> public slots: void StartBtnClicked(); void timeout(); private: int port; bool isStarted; QUdpSocket *udpSocket; QTimer *timer;

(3)在源文件“udpserver.cpp”中添加聲明:

#include <QHostAddress>

其中,在構造函數中添加如下代碼:

connect(StartBtn,SIGNAL(clicked()),this,SLOT(StartBtnClicked())); port = 5555; //設置UDP的端口號參數,服務器定時向此端 口發送廣播信息 isStarted = false; udpSocket = new QUdpSocket(this); //創建一個QUdpSocket timer = new QTimer(this); //定時發送廣播信息 connect(timer,SIGNAL(timeout()),this,SLOT(timeout()));

StartBtnClicked()函數的具體代碼如下:

void UdpServer::StartBtnClicked() { if(!isStarted) { StartBtn->setText(tr("停止")); timer->start(1000); isStarted =true; } else { StartBtn->setText(tr("開始")); isStarted = false; timer->stop(); } }

timeout()函數完成了向端口發送廣播信息的功能,其具體代碼如下:

void UdpServer::timeout() { QString msg = TextLineEdit->text(); int length=0; if(msg=="") { return; } if((length=udpSocket->writeDatagram(msg.toLatin1(), msg.length(),QHostAddress::Broadcast,port))!=msg.length()) { return; } }

四、UDP客戶端編程實例

客戶端的編程具體代碼如下:
(1)在頭文件“udpclient.h”中聲明了需要的各種控件,其具體代碼如下:

#include <QDialog> #include <QVBoxLayout> #include <QTextEdit> #include <QPushButton> class UdpClient : public QDialog { Q_OBJECT public: UdpClient(QWidget *parent = 0,Qt::WindowFlags f=0); ~UdpClient(); private: QTextEdit *ReceiveTextEdit; QPushButton *CloseBtn; QVBoxLayout *mainLayout; };

(2)源文件“udpclient.cpp”的具體代碼如下:

#include "udpclient.h" UdpClient::UdpClient(QWidget *parent, Qt::WindowFlags f) : QDialog(parent,f) { setWindowTitle(tr("UDP Client")); //設置窗體的標題 /* 初始化各個控件 */ ReceiveTextEdit = new QTextEdit(this); CloseBtn = new QPushButton(tr("Close"),this); /* 設置布局 */ mainLayout=new QVBoxLayout(this); mainLayout->addWidget(ReceiveTextEdit); mainLayout->addWidget(CloseBtn); }

(3)客戶端界面如下圖所示:

以上只是完成了客戶端界面的實現,下面完成它的數據接收和顯示的功能。
操作步驟如下。
(1)在“UdpClient.pro”中添加如下語句:

QT += network

(2)在頭文件“udpclient.h”中添加如下代碼:

#include <QUdpSocket> public slots: void CloseBtnClicked(); void dataReceived(); private: int port; QUdpSocket *udpSocket;

(3)在源文件“udpclient.cpp”中添加如下聲明:

#include <QMessageBox> #include <QHostAddress>

其中,在構造函數中添加的代碼如下:

connect(CloseBtn,SIGNAL(clicked()),this,SLOT(CloseBtnClicked())); port =5555; //設置UDP的端口號參數,指定在此端口上監聽數據 udpSocket = new QUdpSocket(this); //創建一個QUdpSocket connect(udpSocket,SIGNAL(readyRead()),this,SLOT(dataReceived())); bool result=udpSocket->bind(port); //綁定到指定的端口上 if(!result) { QMessageBox::information(this,tr("error"),tr("udp socket create error!")); return; }

CloseBtnClicked()函數的具體內容如下:

void UdpClient::CloseBtnClicked() { close(); }

dataReceived()函數響應QUdpSocket的readyRead()信號,一旦UdpSocket對象中有數據可讀時,即
通過readDatagram()方法將數據讀出并顯示。其具體代碼如下:

void UdpClient::dataReceived() { while(udpSocket->hasPendingDatagrams()) { QByteArray datagram; datagram.resize(udpSocket->pendingDatagramSize()); udpSocket->readDatagram(datagram.data(),datagram.size()); QString msg=datagram.data(); ReceiveTextEdit->insertPlainText(msg); //顯示數據內容 } }

同時運行服務器端與客戶端工程,首先在服務器界面的文本框中輸入“hello!”,然后單擊“開始”按鈕,按鈕文本變為“停止”,客戶端就開始不斷地收到“hello!”字符消息并顯示在文本區,當單擊服務器的“停止”按鈕后,按鈕文本又變回“開始”,客戶端也就停止了字符的顯示,再次單擊服務器的“開始”按鈕,客戶端又繼續接收并顯示,如此循環往復,效果如下圖所示。

總結

以上是生活随笔為你收集整理的QT学习:基于UDP的网络广播程序的全部內容,希望文章能夠幫你解決所遇到的問題。

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