|
|
@ -1,4 +1,5 @@
|
|
|
|
#include "CUdpSocket.h"
|
|
|
|
#include "CUdpSocket.h"
|
|
|
|
|
|
|
|
#include "../global.h"
|
|
|
|
|
|
|
|
|
|
|
|
CUdpSocket::CUdpSocket(QObject* parent) : QObject(parent)
|
|
|
|
CUdpSocket::CUdpSocket(QObject* parent) : QObject(parent)
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -10,6 +11,11 @@ CUdpSocket::CUdpSocket(QObject* parent) : QObject(parent)
|
|
|
|
|
|
|
|
|
|
|
|
CUdpSocket::~CUdpSocket()
|
|
|
|
CUdpSocket::~CUdpSocket()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
//离开所有加入的组播组
|
|
|
|
|
|
|
|
if (m_sock->state() == QAbstractSocket::BoundState) {
|
|
|
|
|
|
|
|
m_sock->leaveMulticastGroup(m_hostAddr);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 释放UDP套接字内存空间
|
|
|
|
// 释放UDP套接字内存空间
|
|
|
|
m_sock->close();
|
|
|
|
m_sock->close();
|
|
|
|
delete m_sock;
|
|
|
|
delete m_sock;
|
|
|
@ -18,7 +24,10 @@ CUdpSocket::~CUdpSocket()
|
|
|
|
bool CUdpSocket::bind(QString ip, ushort port)
|
|
|
|
bool CUdpSocket::bind(QString ip, ushort port)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// 返回绑定函数返回值
|
|
|
|
// 返回绑定函数返回值
|
|
|
|
|
|
|
|
m_localIP = ip;
|
|
|
|
|
|
|
|
m_localAddr = QHostAddress(ip);
|
|
|
|
|
|
|
|
m_localPort = port;
|
|
|
|
|
|
|
|
|
|
|
|
if (!m_sock->bind(QHostAddress(ip), port))
|
|
|
|
if (!m_sock->bind(QHostAddress(ip), port))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
qDebug() << "Bind failed:" << m_sock->errorString();
|
|
|
|
qDebug() << "Bind failed:" << m_sock->errorString();
|
|
|
@ -40,13 +49,18 @@ void CUdpSocket::sendData(QByteArray data)
|
|
|
|
|
|
|
|
|
|
|
|
void CUdpSocket::setTargetInfo(QString ip, quint16 port)
|
|
|
|
void CUdpSocket::setTargetInfo(QString ip, quint16 port)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// 存储传入的IP和端口号
|
|
|
|
//存储传入的IP和端口号
|
|
|
|
m_hostAddr = QHostAddress(ip);
|
|
|
|
m_hostAddr = QHostAddress(ip);
|
|
|
|
m_port = port;
|
|
|
|
m_port = port;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool CUdpSocket::close()
|
|
|
|
bool CUdpSocket::close()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
//离开所有加入的组播组
|
|
|
|
|
|
|
|
if (m_sock->state() == QAbstractSocket::BoundState) {
|
|
|
|
|
|
|
|
m_sock->leaveMulticastGroup(m_hostAddr);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
m_sock->close();
|
|
|
|
m_sock->close();
|
|
|
|
if (m_sock->state() == QUdpSocket::UnconnectedState) {
|
|
|
|
if (m_sock->state() == QUdpSocket::UnconnectedState) {
|
|
|
|
qDebug() << "Socket 已成功关闭";
|
|
|
|
qDebug() << "Socket 已成功关闭";
|
|
|
@ -57,16 +71,35 @@ bool CUdpSocket::close()
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool CUdpSocket::joinMulticast()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
bool a = isMulticastAddress(m_localIP);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (m_localIP.isNull() || !isMulticastAddress(m_localIP)) { return false; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!m_sock->joinMulticastGroup(m_localAddr)) {
|
|
|
|
|
|
|
|
qDebug() << "Join multicast group failed:" << m_sock->errorString();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QPair<QString, quint16> CUdpSocket::getSenderIPSetting()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return qMakePair(m_localIP, m_localPort);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CUdpSocket::on_readyReadData()
|
|
|
|
void CUdpSocket::on_readyReadData()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// 通过函数判断当前是否有等待读取的数据并循环获取
|
|
|
|
//通过函数判断当前是否有等待读取的数据并循环获取
|
|
|
|
while (m_sock->hasPendingDatagrams())
|
|
|
|
while (m_sock->hasPendingDatagrams())
|
|
|
|
{
|
|
|
|
{
|
|
|
|
//创建数据存储容器,并设置长度为将接收的数据长度
|
|
|
|
//创建数据存储容器,并设置长度为将接收的数据长度
|
|
|
|
QByteArray data;
|
|
|
|
QByteArray data;
|
|
|
|
data.resize(m_sock->pendingDatagramSize());
|
|
|
|
data.resize(m_sock->pendingDatagramSize());
|
|
|
|
|
|
|
|
|
|
|
|
//读取数据并保存信息发送者的地址和ip(方便发送时指定发送位置)
|
|
|
|
//读取数据并保存信息发送者的地址和ip(方便发送时指定发送位置)
|
|
|
|
m_sock->readDatagram(data.data(), data.size(), &m_hostAddr, &m_port);
|
|
|
|
m_sock->readDatagram(data.data(), data.size(), &senderIP, &senderPort);
|
|
|
|
//发送接收数据的信号
|
|
|
|
//发送接收数据的信号
|
|
|
|
emit recvDataSignal(data);
|
|
|
|
emit recvDataSignal(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|