QtNetwork (examples) - update multicastreceiver example
... as soon as we updated multicastsender. Changes are minimal and mostly cosmetic - use 'explcit' and 'nullptr' where appropriate, make a socket data-member and not a pointer, move the 'datagram's declaration outside of a loop. Task-number: QTBUG-60628 Change-Id: Icfa46e6d2844c40a605f2f4066847594943a8ea8 Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io> Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
This commit is contained in:
parent
36af37c99c
commit
553b6ab9cd
@ -1,6 +1,6 @@
|
|||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
**
|
**
|
||||||
** Copyright (C) 2016 The Qt Company Ltd.
|
** Copyright (C) 2017 The Qt Company Ltd.
|
||||||
** Contact: https://www.qt.io/licensing/
|
** Contact: https://www.qt.io/licensing/
|
||||||
**
|
**
|
||||||
** This file is part of the examples of the Qt Toolkit.
|
** This file is part of the examples of the Qt Toolkit.
|
||||||
@ -54,41 +54,39 @@
|
|||||||
#include "receiver.h"
|
#include "receiver.h"
|
||||||
|
|
||||||
Receiver::Receiver(QWidget *parent)
|
Receiver::Receiver(QWidget *parent)
|
||||||
: QDialog(parent)
|
: QDialog(parent),
|
||||||
|
groupAddress(QStringLiteral("239.255.43.21"))
|
||||||
{
|
{
|
||||||
groupAddress = QHostAddress("239.255.43.21");
|
|
||||||
|
|
||||||
statusLabel = new QLabel(tr("Listening for multicasted messages"));
|
statusLabel = new QLabel(tr("Listening for multicasted messages"));
|
||||||
quitButton = new QPushButton(tr("&Quit"));
|
auto quitButton = new QPushButton(tr("&Quit"));
|
||||||
|
|
||||||
udpSocket = new QUdpSocket(this);
|
auto buttonLayout = new QHBoxLayout;
|
||||||
udpSocket->bind(QHostAddress::AnyIPv4, 45454, QUdpSocket::ShareAddress);
|
|
||||||
udpSocket->joinMulticastGroup(groupAddress);
|
|
||||||
|
|
||||||
connect(udpSocket, SIGNAL(readyRead()),
|
|
||||||
this, SLOT(processPendingDatagrams()));
|
|
||||||
connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
|
|
||||||
|
|
||||||
QHBoxLayout *buttonLayout = new QHBoxLayout;
|
|
||||||
buttonLayout->addStretch(1);
|
buttonLayout->addStretch(1);
|
||||||
buttonLayout->addWidget(quitButton);
|
buttonLayout->addWidget(quitButton);
|
||||||
buttonLayout->addStretch(1);
|
buttonLayout->addStretch(1);
|
||||||
|
|
||||||
QVBoxLayout *mainLayout = new QVBoxLayout;
|
auto mainLayout = new QVBoxLayout;
|
||||||
mainLayout->addWidget(statusLabel);
|
mainLayout->addWidget(statusLabel);
|
||||||
mainLayout->addLayout(buttonLayout);
|
mainLayout->addLayout(buttonLayout);
|
||||||
setLayout(mainLayout);
|
setLayout(mainLayout);
|
||||||
|
|
||||||
setWindowTitle(tr("Multicast Receiver"));
|
setWindowTitle(tr("Multicast Receiver"));
|
||||||
|
|
||||||
|
udpSocket.bind(QHostAddress::AnyIPv4, 45454, QUdpSocket::ShareAddress);
|
||||||
|
udpSocket.joinMulticastGroup(groupAddress);
|
||||||
|
|
||||||
|
connect(&udpSocket, SIGNAL(readyRead()),
|
||||||
|
this, SLOT(processPendingDatagrams()));
|
||||||
|
connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Receiver::processPendingDatagrams()
|
void Receiver::processPendingDatagrams()
|
||||||
{
|
{
|
||||||
while (udpSocket->hasPendingDatagrams()) {
|
QByteArray datagram;
|
||||||
QByteArray datagram;
|
while (udpSocket.hasPendingDatagrams()) {
|
||||||
datagram.resize(udpSocket->pendingDatagramSize());
|
datagram.resize(int(udpSocket.pendingDatagramSize()));
|
||||||
udpSocket->readDatagram(datagram.data(), datagram.size());
|
udpSocket.readDatagram(datagram.data(), datagram.size());
|
||||||
statusLabel->setText(tr("Received datagram: \"%1\"")
|
statusLabel->setText(tr("Received datagram: \"%1\"")
|
||||||
.arg(datagram.data()));
|
.arg(datagram.constData()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
**
|
**
|
||||||
** Copyright (C) 2016 The Qt Company Ltd.
|
** Copyright (C) 2017 The Qt Company Ltd.
|
||||||
** Contact: https://www.qt.io/licensing/
|
** Contact: https://www.qt.io/licensing/
|
||||||
**
|
**
|
||||||
** This file is part of the examples of the Qt Toolkit.
|
** This file is part of the examples of the Qt Toolkit.
|
||||||
@ -53,11 +53,10 @@
|
|||||||
|
|
||||||
#include <QDialog>
|
#include <QDialog>
|
||||||
#include <QHostAddress>
|
#include <QHostAddress>
|
||||||
|
#include <QUdpSocket>
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
QT_BEGIN_NAMESPACE
|
||||||
class QLabel;
|
class QLabel;
|
||||||
class QPushButton;
|
|
||||||
class QUdpSocket;
|
|
||||||
QT_END_NAMESPACE
|
QT_END_NAMESPACE
|
||||||
|
|
||||||
class Receiver : public QDialog
|
class Receiver : public QDialog
|
||||||
@ -65,15 +64,14 @@ class Receiver : public QDialog
|
|||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Receiver(QWidget *parent = 0);
|
explicit Receiver(QWidget *parent = nullptr);
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void processPendingDatagrams();
|
void processPendingDatagrams();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QLabel *statusLabel;
|
QLabel *statusLabel = nullptr;
|
||||||
QPushButton *quitButton;
|
QUdpSocket udpSocket;
|
||||||
QUdpSocket *udpSocket;
|
|
||||||
QHostAddress groupAddress;
|
QHostAddress groupAddress;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user