QT UPD功能实现类
QT封装了QUdpSocket 类用于实现UDP通信功能可点击官方说明查看。
二次封装说明
服务器类UdpServer封装了const bool sendDate(const QByteArray& data)
函数进行数据发送,客户端类UpdClient封装了槽函数void dataRecived(void)
来进行数据接收。可根据实际需要对这两个函数进行修改从而实现具体需求功能。
功能代码
udpServer.h
#pragma once
#include <QUdpSocket>
#include <QMutex>
class UdpServer
{
private:
QUdpSocket mUdpSocket;
QMutex mUdpSocketMutex;
QHostAddress mHostAddr;
quint16 mPort;
public:
UdpServer(const QHostAddress& hostAddr, const quint16& port);
~UdpServer(void) {
};
/*
@brife 发送数据
@param data 数据
@return true 发送成功;false 发送失败
*/
const bool sendDate(const QByteArray& data);
};
udpServer.cpp
#include "udpServer.h"
UdpServer::UdpServer(const QHostAddress& hostAddr, const quint16& port):mHostAddr(hostAddr), mPort(port)
{
}