UdpSocket::UdpSocket(QObject *parent) : QObject(parent) { //本机IP QString local_ip = "192.168.101.11"; m_udp_socket = new QUdpSocket(this); connect(m_udp_socket, &QUdpSocket::readyRead, this, &UdpSocket::received_data); int ret = m_udp_socket->bind(QHostAddress(local_ip), MULTI_CAST_PORT, QUdpSocket::ShareAddress); if (!ret) { qDebug()<<"bind multi cast failed..."; } QHostAddress multi_address(MULTI_CAST_IP); bool join_result = m_udp_socket->joinMulticastGroup(multi_address); if (!join_result) { qDebug() << "add multi cast group failed..."; } } void UdpSocket::received_data() { QByteArray buffer; buffer.resize(m_udp_socket->pendingDatagramSize()); m_udp_socket->readDatagram(buffer.data(), buffer.size()); qDebug() << "buffer:" << buffer; }
标签:多播,UDP,组播,socket,ip,udp,QUdpSocket,qDebug,UdpSocket From: https://www.cnblogs.com/TechNomad/p/17870860.html