赞
踩
Internet 协议集支持一个无连接的传输协议,该协议称为用户数据报协议(UDP,User Datagram Protocol)。UDP 为应用程序提供了一种无需建立连接就可以发送封装的 IP 数据包的方法。RFC 768 描述了 UDP。
UDP协议根据消息传送模式可以分为:单播(Unicast)、组播(Multicast)和广播(Broadcast)。
Qt中提供了QUdpSocket类用于创建UDP套接字。
QUdpSocket类继承于QAbstractSocket,提供了UdpSocket套接字的创建、连接对位服务器、加组等。
UDP通信是对等服务器间信息传递,其实不需要指定客户端和服务器端,但是为了便于理解,我在实现时仍然采用C-S的结构。
实现步骤如下:
创建UDP套接字 --> 绑定端口 --> 加组(区分消息传送模式) -->发送数据(区分消息传送模式) --> 接受数据
客户端实现发送数据。
#ifndef UDPCLIENT_H #define UDPCLIENT_H #include <QObject> #include <QHostAddress> #include <QUdpSocket> #include <QDebug> #include <QTimer> class UDPClient :QObject { Q_OBJECT public: UDPClient(); void InitSocket();//初始化UDP套接字 void InitTimer();//初始化定时器 public slots: void SendData();//发送数据 private: QUdpSocket *mUdpSocket;//UDP套接字对象 QHostAddress mGroupAddress;//组播地址 QTimer *mTimer;//定时器对象 int mType;//记录UDP消息传送模式 0:单播 1:广播 2:组播(多播) }; #endif // UDPCLIENT_H
#include "udpclient.h" UDPClient::UDPClient() { // mType = 0;//Unicast // mType = 1;//Broadcast mType = 2;//Multicast InitSocket(); InitTimer(); } void UDPClient::InitSocket() { mUdpSocket = new QUdpSocket;//初始化socket mGroupAddress.setAddress("239.2.2.222");//设置组播地址 mUdpSocket->bind(6666);//绑定端口号 if(mType == 2) { //组播的数据的生存期,数据报没跨1个路由就会减1.表示多播数据报只能在同一路由下的局域网内传播 mUdpSocket->setSocketOption(QAbstractSocket::MulticastTtlOption,1); } } void UDPClient::InitTimer() { mTimer = new QTimer;//初始化定时器 connect(mTimer,&QTimer::timeout,this,[=] { SendData(); }); mTimer->start(1000);//每隔一秒发送一次数据 } void UDPClient::SendData() { QByteArray _data = "hello"; if(mType == 0)//单播 { QHostAddress _peerHostAddress = QHostAddress("10.0.0.177");//对位服务器IP quint16 _port = 6666;//对位服务器端口 if(-1 !=mUdpSocket->writeDatagram(_data.data(),_data.size(),_peerHostAddress,_port)) { qDebug()<< "Unicast ==> Send data : "<< _data<<endl; } mUdpSocket->flush(); } else if(mType == 1)//广播 { quint16 _port = 6666;//广播端口 if(-1 !=mUdpSocket->writeDatagram(_data.data(),QHostAddress::Broadcast,_port)) { qDebug()<< "Broadcast ==> Send data : "<< _data<<endl; } mUdpSocket->flush(); } else if(mType == 2)//组播 { quint16 _port = 8888;//组播端口 if(-1 != mUdpSocket->writeDatagram(_data.data(),mGroupAddress,_port)) { qDebug()<< "Multicast ==> Send data : "<< _data<<endl; } mUdpSocket->flush(); } else { qDebug()<< "mType is error! "<<endl; return; } }
服务器端实现数据的接收。
#ifndef UDPSERVER_H #define UDPSERVER_H #include <QObject> #include <QHostAddress> #include <QUdpSocket> #include <QDebug> class UDPServer : QObject { Q_OBJECT public: UDPServer(); void InitSocket();//初始化套接字 public slots: void ReadPendingDataframs();//读取消息 private: QUdpSocket *mUdpSocket;//UDP套接字 QHostAddress mGroupAdress;//组播地址 int mType; //记录UDP消息传送模式 0:单播 1:广播 2:组播(多播) }; #endif // UDPSERVER_H
#include "udpserver.h" UDPServer::UDPServer() { // mType = 0;//Unicast // mType = 1;//Broadcast mType = 2;//Multicast InitSocket(); } void UDPServer::InitSocket() { //初始化socket,设置组播地址 mUdpSocket = new QUdpSocket; mGroupAdress.setAddress("239.2.2.222"); if(mType == 0 || mType == 1) { //绑定本地IP和端口号 mUdpSocket->bind(6666); } else if(mType == 2) { if(mUdpSocket->bind(QHostAddress::AnyIPv4,8888,QUdpSocket::ShareAddress)) { //加入组播地址 mUdpSocket->joinMulticastGroup(mGroupAdress); qDebug()<<("Join Multicast Adrress [")<<mGroupAdress.toString() <<("] Successful!")<<endl; } } else { qDebug()<< "mType is error! "<<endl; return; } connect(mUdpSocket,&QUdpSocket::readyRead,this,[=]{ ReadPendingDataframs(); }); } void UDPServer::ReadPendingDataframs() { QByteArray _data; _data.resize(mUdpSocket->pendingDatagramSize()); if(mType == 0)//Unicast { QHostAddress *_peerHostAddress = new QHostAddress("10.0.0.32"); quint16 _port = 6666; while(mUdpSocket->hasPendingDatagrams()) { mUdpSocket->readDatagram(_data.data(),_data.size(),_peerHostAddress,&_port);//接收指定IP和端口的udp报文 qDebug()<<"Unicast ==> Receive data : "<<QString::fromLatin1(_data)<<endl; } } else if(mType == 1)//Broadcast { QHostAddress _peerHostAddress; quint16 _port; while(mUdpSocket->hasPendingDatagrams()) { mUdpSocket->readDatagram(_data.data(),_data.size(),&_peerHostAddress,&_port);//接收同一子网的udp报文 qDebug()<<"Broadcast ==> Receive data : "<<QString::fromLatin1(_data)<<endl; } } else if(mType == 2)//Multicast { QHostAddress _peerHostAddress; quint16 _port; while(mUdpSocket->hasPendingDatagrams()) { mUdpSocket->readDatagram(_data.data(),_data.size(),&_peerHostAddress,&_port);//接收同组的udp报文 qDebug()<<"Multicast ==> Receive data : "<<QString::fromLatin1(_data)<<endl; } } else { qDebug()<< "mType is error! "<<endl; return; } }
以上就是QT实现UDP协议通信的全部内容了,记得加上network模块:)
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。