UDP 是一個不可靠的,面向數據報的協議。QUdpSocket 類可以用來發送和接收UDP數據報(datagram)。
最常用的使用方式是使用bind()去綁定地址和端口號,然后使用writeDatagram()和readDatagram()去傳輸數據。
這個socket對象每次往網絡中發送報文都會發出bytesWritten()信號。如果你只是想用QUdpSocket發送報文,就不需要調用bind().
當報文到達的時候會發readyRead()信號,在這種情況下,hasPendingDatagrams()會返回true.調用 pendingDatagramSize()方法獲取報文的長度。最后調用readDatagram()讀取。
下面的實例WeatherServery應用程序模擬氣象氣球的功能,每2秒就發送一個包含當前天氣情況的UDP數據報。
1 #ifndef WEATHERBALLOON_H 2 #define WEATHERBALLOON_H 3 4 #include <QWidget> 5 #include <QPushButton> 6 #include <QtNetwork/QUdpSocket> 7 #include <QTimer> 8 #include <QDateTime> 9 namespace Ui { 10 class weatherBalloon; 11 } 12 13 class weatherBalloon : public QWidget 14 { 15 Q_OBJECT 16 17 public: 18 explicit weatherBalloon(QWidget *parent = 0); 19 ~weatherBalloon(); 20 21 private slots: 22 //處理報文 23 void processPendingDatagrams(); 24 //發送報文 25 void sendDatagram(); 26 private: 27 Ui::weatherBalloon *ui; 28 QUdpSocket udpSocket; 29 QTimer timer; 30 double temperature;//溫度 31 double humidity;//濕度 32 double altitude;//高度 33 }; 34 35 #endif // WEATHERBALLOON_H
WeatherServer的實現
1 #include "weatherballoon.h" 2 #include "ui_weatherballoon.h" 3 4 weatherBalloon::weatherBalloon(QWidget *parent) : 5 QWidget(parent), 6 ui(new Ui::weatherBalloon) 7 { 8 //綁定Socket到指定地址和端口號 9 udpSocket.bind(5824); 10 11 ui->setupUi(this); 12 connect(ui->btn_close,SIGNAL(clicked()),this,SLOT(close())); 13 connect(&timer,SIGNAL(timeout()),this,SLOT(sendDatagram())); 14 connect(&udpSocket,SIGNAL(readyRead()),this,SLOT(processPendingDatagrams())); 15 16 timer.start(2*1000); 17 temperature = 10.2; 18 humidity = 5.4; 19 altitude = 100.0; 20 setWindowTitle(tr("Weather Balloon")); 21 } 22 23 weatherBalloon::~weatherBalloon() 24 { 25 delete ui; 26 } 27 28 //發送報文 29 void weatherBalloon::sendDatagram(){ 30 QByteArray datagram; 31 QDataStream out(&datagram,QIODevice::WriteOnly); 32 out.setVersion(QDataStream::Qt_4_8); 33 out<<QDateTime::currentDateTime()<<temperature<<humidity<<altitude; 34 qDebug()<<QDateTime::currentDateTime(); 35 QHostAddress address; 36 address.setAddress("127.0.0.1"); 37 udpSocket.writeDatagram(datagram,address,5824); 38 } 39 40 //處理報文 41 void weatherBalloon::processPendingDatagrams(){ 42 QByteArray datagram; 43 do{ 44 datagram.resize(udpSocket.pendingDatagramSize()); 45 udpSocket.readDatagram(datagram.data(),datagram.size()); 46 }while(udpSocket.hasPendingDatagrams()); 47 QDateTime dateTime; 48 double temperature; 49 double humidity; 50 double altitude; 51 qDebug()<<"recive date "; 52 QDataStream in(&datagram,QIODevice::ReadOnly); 53 in.setVersion(QDataStream::Qt_4_8); 54 in>>dateTime>>temperature>>humidity>>altitude; 55 56 57 ui->lineEdit_Date->setText(dateTime.date().toString()); 58 ui->lineEdit_CurrentTime->setText(dateTime.time().toString()); 59 ui->lineEdit_Temperature->setText(tr("%1 °c").arg(temperature)); 60 ui->lineEdit_Humidity->setText(tr("%1%").arg(humidity)); 61 ui->lineEdit_Alt->setText(tr("%1 m").arg(altitude)); 62 63 }
參考資料:
C++ GUI Qt4 編程(第二版)P87
Qt 幫助文檔中關於QUdpSocket
源碼下載:http://download.csdn.net/detail/gongchao1212/9731989
