Skip to content

Commit

Permalink
QtNetwork (examples) - update multicastreceiver example
Browse files Browse the repository at this point in the history
... 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 <[email protected]>
Reviewed-by: Edward Welbourne <[email protected]>
  • Loading branch information
Timur Pocheptsov committed Sep 28, 2017
1 parent 36af37c commit 553b6ab
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 27 deletions.
38 changes: 18 additions & 20 deletions examples/network/multicastreceiver/receiver.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
Expand Down Expand Up @@ -54,41 +54,39 @@
#include "receiver.h"

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"));
quitButton = new QPushButton(tr("&Quit"));

udpSocket = new QUdpSocket(this);
udpSocket->bind(QHostAddress::AnyIPv4, 45454, QUdpSocket::ShareAddress);
udpSocket->joinMulticastGroup(groupAddress);

connect(udpSocket, SIGNAL(readyRead()),
this, SLOT(processPendingDatagrams()));
connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
auto quitButton = new QPushButton(tr("&Quit"));

QHBoxLayout *buttonLayout = new QHBoxLayout;
auto buttonLayout = new QHBoxLayout;
buttonLayout->addStretch(1);
buttonLayout->addWidget(quitButton);
buttonLayout->addStretch(1);

QVBoxLayout *mainLayout = new QVBoxLayout;
auto mainLayout = new QVBoxLayout;
mainLayout->addWidget(statusLabel);
mainLayout->addLayout(buttonLayout);
setLayout(mainLayout);

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()
{
while (udpSocket->hasPendingDatagrams()) {
QByteArray datagram;
datagram.resize(udpSocket->pendingDatagramSize());
udpSocket->readDatagram(datagram.data(), datagram.size());
QByteArray datagram;
while (udpSocket.hasPendingDatagrams()) {
datagram.resize(int(udpSocket.pendingDatagramSize()));
udpSocket.readDatagram(datagram.data(), datagram.size());
statusLabel->setText(tr("Received datagram: \"%1\"")
.arg(datagram.data()));
.arg(datagram.constData()));
}
}
12 changes: 5 additions & 7 deletions examples/network/multicastreceiver/receiver.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
Expand Down Expand Up @@ -53,27 +53,25 @@

#include <QDialog>
#include <QHostAddress>
#include <QUdpSocket>

QT_BEGIN_NAMESPACE
class QLabel;
class QPushButton;
class QUdpSocket;
QT_END_NAMESPACE

class Receiver : public QDialog
{
Q_OBJECT

public:
Receiver(QWidget *parent = 0);
explicit Receiver(QWidget *parent = nullptr);

private slots:
void processPendingDatagrams();

private:
QLabel *statusLabel;
QPushButton *quitButton;
QUdpSocket *udpSocket;
QLabel *statusLabel = nullptr;
QUdpSocket udpSocket;
QHostAddress groupAddress;
};

Expand Down

0 comments on commit 553b6ab

Please sign in to comment.