diff options
author | Thiago Macieira <thiago.macieira@intel.com> | 2017-12-12 13:55:32 -0800 |
---|---|---|
committer | Thiago Macieira <thiago.macieira@intel.com> | 2017-12-24 02:04:07 +0000 |
commit | f9c07da7b6c167820a71d2c840b7be70c393457a (patch) | |
tree | ff29f47498f6f6a95167acc65cb9e8d40a21a7f6 /examples/network/multicastreceiver | |
parent | c5a3022b041f19c3bc2dd2204c9f9b334c7f986f (diff) |
Examples: Update multicast sender and receiver examples for IPv6
It's the right thing to do, as we're in 2017, not 1997. Also, this takes
care to indicate that QAbstractSocket::MulticastTtlOption makes sense
mostly for IPv4, even though it's implemented for both families. In
IPv4, it's used to indicatae the scope, whereas in IPv6 it's stored in
bits 12-15 of the address.
Task-number: QTBUG-46046
Change-Id: I9741f017961b410c910dfffd14ffaabe0a2024d8
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
Diffstat (limited to 'examples/network/multicastreceiver')
-rw-r--r-- | examples/network/multicastreceiver/receiver.cpp | 35 | ||||
-rw-r--r-- | examples/network/multicastreceiver/receiver.h | 6 |
2 files changed, 30 insertions, 11 deletions
diff --git a/examples/network/multicastreceiver/receiver.cpp b/examples/network/multicastreceiver/receiver.cpp index 8985ad1d82..d793242ad0 100644 --- a/examples/network/multicastreceiver/receiver.cpp +++ b/examples/network/multicastreceiver/receiver.cpp @@ -55,9 +55,10 @@ Receiver::Receiver(QWidget *parent) : QDialog(parent), - groupAddress(QStringLiteral("239.255.43.21")) + groupAddress4(QStringLiteral("239.255.43.21")), + groupAddress6(QStringLiteral("ff12::2115")) { - statusLabel = new QLabel(tr("Listening for multicasted messages")); + statusLabel = new QLabel(tr("Listening for multicast messages on both IPv4 and IPv6")); auto quitButton = new QPushButton(tr("&Quit")); auto buttonLayout = new QHBoxLayout; @@ -72,21 +73,37 @@ Receiver::Receiver(QWidget *parent) setWindowTitle(tr("Multicast Receiver")); - udpSocket.bind(QHostAddress::AnyIPv4, 45454, QUdpSocket::ShareAddress); - udpSocket.joinMulticastGroup(groupAddress); + udpSocket4.bind(QHostAddress::AnyIPv4, 45454, QUdpSocket::ShareAddress); + udpSocket4.joinMulticastGroup(groupAddress4); - connect(&udpSocket, SIGNAL(readyRead()), + if (!udpSocket6.bind(QHostAddress::AnyIPv6, 45454, QUdpSocket::ShareAddress) || + !udpSocket6.joinMulticastGroup(groupAddress6)) + statusLabel->setText(tr("Listening for multicast messages on IPv4 only")); + + connect(&udpSocket4, SIGNAL(readyRead()), this, SLOT(processPendingDatagrams())); + connect(&udpSocket6, &QUdpSocket::readyRead, this, &Receiver::processPendingDatagrams); connect(quitButton, SIGNAL(clicked()), this, SLOT(close())); } void Receiver::processPendingDatagrams() { QByteArray datagram; - while (udpSocket.hasPendingDatagrams()) { - datagram.resize(int(udpSocket.pendingDatagramSize())); - udpSocket.readDatagram(datagram.data(), datagram.size()); - statusLabel->setText(tr("Received datagram: \"%1\"") + + // using QUdpSocket::readDatagram (API since Qt 4) + while (udpSocket4.hasPendingDatagrams()) { + datagram.resize(int(udpSocket4.pendingDatagramSize())); + udpSocket4.readDatagram(datagram.data(), datagram.size()); + statusLabel->setText(tr("Received IPv4 datagram: \"%1\"") .arg(datagram.constData())); } + + // using QUdpSocket::receiveDatagram (API since Qt 5.8) + while (udpSocket6.hasPendingDatagrams()) { + QNetworkDatagram dgram = udpSocket6.receiveDatagram(); + statusLabel->setText(statusLabel->text() + + tr("\nReceived IPv6 datagram from [%2]:%3: \"%1\"") + .arg(dgram.data().constData(), dgram.senderAddress().toString()) + .arg(dgram.senderPort())); + } } diff --git a/examples/network/multicastreceiver/receiver.h b/examples/network/multicastreceiver/receiver.h index 54927fdd63..0325d861df 100644 --- a/examples/network/multicastreceiver/receiver.h +++ b/examples/network/multicastreceiver/receiver.h @@ -71,8 +71,10 @@ private slots: private: QLabel *statusLabel = nullptr; - QUdpSocket udpSocket; - QHostAddress groupAddress; + QUdpSocket udpSocket4; + QUdpSocket udpSocket6; + QHostAddress groupAddress4; + QHostAddress groupAddress6; }; #endif |