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