码迷,mamicode.com
首页 > 其他好文 > 详细

qt 组播

时间:2016-08-11 17:48:18      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QThread>


#define UDP_PORT1   31002

QHostAddress MulticastGroup = QHostAddress("239.192.1.1");


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    setWindowTitle("udp test");

    udp10002 = new QUdpSocket(this);
    udp10002->bind(QHostAddress::AnyIPv4, UDP_PORT1,QAbstractSocket::ShareAddress);
    udp10002->joinMulticastGroup(MulticastGroup);
    udp10002->setSocketOption( udp10002->MulticastLoopbackOption,0);

    connect(udp10002, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams10002()));
}

void MainWindow::resizeEvent(QResizeEvent *)
{
    ui->horizontalLayoutWidget->resize(this->width(),this->height());
}

MainWindow::~MainWindow()
{
    delete ui;

    udp10002->close();
    qDebug()<<"close " <<UDP_PORT1;
    udp10002->leaveMulticastGroup(MulticastGroup);
}
void MainWindow::readPendingDatagrams10002()
{
    while (udp10002->hasPendingDatagrams())
    {
        QByteArray datagram;
        datagram.resize(udp10002->pendingDatagramSize());
        QHostAddress sender;
        quint16 senderPort;

        udp10002->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);

         qDebug()<<"read:"<<sender<<senderPort<<datagram;
        udp10002->writeDatagram("HHHHHHHHHHHHHHH",sender,senderPort);
        udp10002->writeDatagram("HHHHHHHHHHHHHHH",MulticastGroup,31003);
    }
}

 

qt 组播

标签:

原文地址:http://www.cnblogs.com/qk2015/p/5761636.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!