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
#include
#include
#include
#include 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;//BroadcastmType = 2;//MulticastInitSocket();InitTimer();}void UDPClient::InitSocket()
{mUdpSocket = new QUdpSocket;//初始化socketmGroupAddress.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");//对位服务器IPquint16 _port = 6666;//对位服务器端口if(-1 !=mUdpSocket->writeDatagram(_data.data(),_data.size(),_peerHostAddress,_port)){qDebug()<< "Unicast ==> Send data : "<< _data<flush();}else if(mType == 1)//广播{quint16 _port = 6666;//广播端口if(-1 !=mUdpSocket->writeDatagram(_data.data(),QHostAddress::Broadcast,_port)){qDebug()<< "Broadcast ==> Send data : "<< _data<flush();}else if(mType == 2)//组播{quint16 _port = 8888;//组播端口if(-1 != mUdpSocket->writeDatagram(_data.data(),mGroupAddress,_port)){qDebug()<< "Multicast ==> Send data : "<< _data<flush();}else{qDebug()<< "mType is error! "<
服务器端实现数据的接收。
#ifndef UDPSERVER_H
#define UDPSERVER_H#include
#include
#include
#include 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;//BroadcastmType = 2;//MulticastInitSocket();}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 [")<qDebug()<< "mType is error! "<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 : "<QHostAddress _peerHostAddress;quint16 _port;while(mUdpSocket->hasPendingDatagrams()){mUdpSocket->readDatagram(_data.data(),_data.size(),&_peerHostAddress,&_port);//接收同一子网的udp报文qDebug()<<"Broadcast ==> Receive data : "<QHostAddress _peerHostAddress;quint16 _port;while(mUdpSocket->hasPendingDatagrams()){mUdpSocket->readDatagram(_data.data(),_data.size(),&_peerHostAddress,&_port);//接收同组的udp报文qDebug()<<"Multicast ==> Receive data : "<qDebug()<< "mType is error! "<
以上就是QT实现UDP协议通信的全部内容了,记得加上network模块:)