源码下载地址: C++、Qt学习交流群: 302558294(欢迎你的加入) 理论: Qt 网络之UDP 网络基础学习是本文要介绍的内容,UDP(User Datagram Protocol即用户数据报协议)是一个轻量级的,不
源码下载地址:
C++、Qt学习交流群: 302558294(欢迎你的加入)
理论:
Qt 网络之UDP 网络基础学习是本文要介绍的内容,UDP(User Datagram Protocol即用户数据报协议)是一个轻量级的,不可靠的,面向数据报的无连接协议 。对于UDP我们不再进行过多介绍,如果你对UDP不是很了解,而且不知道它有什么用,那么我们这里就举个简单的例子:我们现在几乎每个人都使用的腾讯QQ,其聊天时就是使用UDP协议进行消息发送的 。就像QQ那样,当有很多用户,发送的大部分都是短消息,要求能及时响应,并且对安全性要求不是很高的情况下使用UDP协议。
在Qt中提供了QUdpSocket 类来进行UDP数据报(datagrams)的发送和接收 。这里我们还要了解一个名词Socket,也就是常说的“套接字” 。 Socket简单地说,就是一个IP地址加一个port端口 。因为我们要传输数据,就要知道往哪个机子上传送,而IP地址确定了一台主机,但是这台机子上可能运行着各种各样的网络程序,我们要往哪个程序中发送呢?这时就要使用一个端口来指定UDP程序 。所以说,Socket指明了数据报传输的路径。
实例:
功能:server(图右)端每隔一秒钟,想UDP管道中写入数据,client(图左)端从管道中读出数据,并显示到界面上。
效果图:
服务器端源码:
//weatherballon.h
#ifndef WEATHERBALLON_H#define WEATHERBALLON_H#include <QPushButton>#include <QTimer>#include <QUdpSocket>class WeatherBallon : public QPushButton{ Q_OBJECTpublic: WeatherBallon(QWidget *parent = 0); ~WeatherBallon(); double temperature() const; double humidity() const; double altitude() const;private slots: void onSendDatagram();private: QUdpSocket mUdpSocket; QTimer timer;};#endif // WEATHERBALLON_H
//weatherballon.cpp
#include <QDateTime>#include <QIODevice>#include <QByteArray>#include "weatherballon.h"WeatherBallon::WeatherBallon(QWidget *parent) : QPushButton (tr("quit"),parent){ connect(this,SIGNAL(clicked(bool)),this,SLOT(close())); connect(&timer,SIGNAL(timeout()),this,SLOT(onSendDatagram())); timer.start(1000); setWindowTitle(tr("Weather Server"));}WeatherBallon::~WeatherBallon(){}double WeatherBallon::temperature() const{ return -20.0 + (2.0 * std::rand() / (RAND_MAX + 1.0));}double WeatherBallon::humidity() const{ return 20.0 + (2.0 * std::rand() / (RAND_MAX + 1.0));}double WeatherBallon::altitude() const{ return 7000 + (100.0 * std::rand() / (RAND_MAX + 1.0));}void WeatherBallon::onSendDatagram(){ QByteArray datagram; QDataStream out(&datagram, QIODevice::WriteOnly); out.setVersion(QDataStream::Qt_5_7); out<<QDateTime::currentDateTime() <<temperature() << humidity() <<altitude(); mUdpSocket.writeDatagram(datagram,QHostAddress::LocalHost ,5824);}
客户端源码:
//weatherstation.h
#ifndef WEATHERSTATION_H#define WEATHERSTATION_H#include <QDialog>#include <QLabel>#include <QLineEdit>#include <QtNetwork/QUdpSocket>class WeatherStation : public QDialog{ Q_OBJECTpublic: WeatherStation(QWidget *parent = 0); ~WeatherStation();private slots: void onProcessPendingDatagrams();private: QUdpSocket mUdpSocket; QLabel *mDataLabel; QLabel *mTimeLabel; QLabel *mTemperatureLabel; QLabel *mhumidtyLabel; QLabel *mAltitudeLabel; QLineEdit *mDateLineEdit; QLineEdit *mTimeLineEdit; QLineEdit *mTemperatureLineEdit; QLineEdit *mHunidityLineEdit; QLineEdit *mAltitudeLineEdit;};#endif // WEATHERSTATION_H
//weatherstation.cpp
#include <QGridLayout>#include <QTimer>#include <QDateTime>#include "weatherstation.h"WeatherStation::WeatherStation(QWidget *parent) : QDialog(parent){ mUdpSocket.bind(5824); connect(&mUdpSocket,SIGNAL(readyRead()),SLOT(onProcessPendingDatagrams())); mDataLabel = new QLabel(tr("Data:")); mTimeLabel = new QLabel(tr("Time:")); mTemperatureLabel = new QLabel(tr("Temperature:")); mhumidtyLabel = new QLabel(tr("Hunidity:")); mAltitudeLabel = new QLabel(tr("Altitude:")); mDateLineEdit = new QLineEdit; mTimeLineEdit = new QLineEdit; mTemperatureLineEdit = new QLineEdit; mHunidityLineEdit = new QLineEdit; mAltitudeLineEdit = new QLineEdit; mDateLineEdit->setReadOnly(true); mTimeLineEdit->setReadOnly(true); mTemperatureLineEdit->setReadOnly(true); mHunidityLineEdit->setReadOnly(true); mAltitudeLineEdit->setReadOnly(true); QGridLayout *mainLayout = new QGridLayout; mainLayout->addWidget(mDataLabel,0,0); mainLayout->addWidget(mDateLineEdit,0,1); mainLayout->addWidget(mTimeLabel,1,0); mainLayout->addWidget(mTimeLineEdit,1,1); mainLayout->addWidget(mTemperatureLabel,2,0); mainLayout->addWidget(mTemperatureLineEdit,2,1); mainLayout->addWidget(mhumidtyLabel,3,0); mainLayout->addWidget(mHunidityLineEdit,3,1); mainLayout->addWidget(mAltitudeLabel,4,0); mainLayout->addWidget(mAltitudeLineEdit,4,1); setLayout(mainLayout); setWindowTitle(tr("Weather Station"));}WeatherStation::~WeatherStation(){}void WeatherStation::onProcessPendingDatagrams(){ QByteArray datagram; while(mUdpSocket.hasPendingDatagrams()) //是否有数据包等待读取 { datagram.resize(mUdpSocket.pendingDatagramSize()); // mUdpSocket.readDatagram(datagram.data(), datagram.size()); } QDateTime dateTime; double temperature; double humidity; double altitude; QDataStream in(&datagram,QIODevice::ReadOnly); //从datagram中读取数据 in.setVersion(QDataStream::Qt_5_7); in>>dateTime >> temperature >>humidity >>altitude; mDateLineEdit->setText(dateTime.date().toString()); mTimeLineEdit->setText(dateTime.time().toString()); mTemperatureLineEdit->setText(tr("%1 C").arg(temperature)); mHunidityLineEdit->setText(tr("%1%").arg(humidity)); mAltitudeLineEdit->setText(tr("%1 m").arg(altitude));}