summaryrefslogtreecommitdiffstats
path: root/examples/network/multicastsender
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2017-12-12 13:55:32 -0800
committerThiago Macieira <thiago.macieira@intel.com>2017-12-24 02:04:07 +0000
commitf9c07da7b6c167820a71d2c840b7be70c393457a (patch)
treeff29f47498f6f6a95167acc65cb9e8d40a21a7f6 /examples/network/multicastsender
parentc5a3022b041f19c3bc2dd2204c9f9b334c7f986f (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/multicastsender')
-rw-r--r--examples/network/multicastsender/sender.cpp23
-rw-r--r--examples/network/multicastsender/sender.h6
2 files changed, 22 insertions, 7 deletions
diff --git a/examples/network/multicastsender/sender.cpp b/examples/network/multicastsender/sender.cpp
index cb4bf45672..a542a2528f 100644
--- a/examples/network/multicastsender/sender.cpp
+++ b/examples/network/multicastsender/sender.cpp
@@ -52,11 +52,21 @@
Sender::Sender(QWidget *parent)
: QDialog(parent),
- groupAddress(QStringLiteral("239.255.43.21"))
+ groupAddress4(QStringLiteral("239.255.43.21")),
+ groupAddress6(QStringLiteral("ff12::2115"))
{
- statusLabel = new QLabel(tr("Ready to multicast datagrams to group %1 on port 45454").arg(groupAddress.toString()));
+ // force binding to their respective families
+ udpSocket4.bind(QHostAddress(QHostAddress::AnyIPv4), 0);
+ udpSocket6.bind(QHostAddress(QHostAddress::AnyIPv6), udpSocket4.localPort());
- auto ttlLabel = new QLabel(tr("TTL for multicast datagrams:"));
+ QString msg = tr("Ready to multicast datagrams to groups %1 and [%2] on port 45454").arg(groupAddress4.toString());
+ if (udpSocket6.state() != QAbstractSocket::BoundState)
+ msg = tr("IPv6 failed. Ready to multicast datagrams to group %1 on port 45454").arg(groupAddress4.toString());
+ else
+ msg = msg.arg(groupAddress6.toString());
+ statusLabel = new QLabel(msg);
+
+ auto ttlLabel = new QLabel(tr("TTL for IPv4 multicast datagrams:"));
auto ttlSpinBox = new QSpinBox;
ttlSpinBox->setRange(0, 255);
@@ -88,7 +98,8 @@ Sender::Sender(QWidget *parent)
void Sender::ttlChanged(int newTtl)
{
- udpSocket.setSocketOption(QAbstractSocket::MulticastTtlOption, newTtl);
+ // we only set the TTL on the IPv4 socket, as that changes the multicast scope
+ udpSocket4.setSocketOption(QAbstractSocket::MulticastTtlOption, newTtl);
}
void Sender::startSending()
@@ -101,6 +112,8 @@ void Sender::sendDatagram()
{
statusLabel->setText(tr("Now sending datagram %1").arg(messageNo));
QByteArray datagram = "Multicast message " + QByteArray::number(messageNo);
- udpSocket.writeDatagram(datagram, groupAddress, 45454);
+ udpSocket4.writeDatagram(datagram, groupAddress4, 45454);
+ if (udpSocket6.state() == QAbstractSocket::BoundState)
+ udpSocket6.writeDatagram(datagram, groupAddress6, 45454);
++messageNo;
}
diff --git a/examples/network/multicastsender/sender.h b/examples/network/multicastsender/sender.h
index 5d8769790e..0af6f9aaec 100644
--- a/examples/network/multicastsender/sender.h
+++ b/examples/network/multicastsender/sender.h
@@ -70,9 +70,11 @@ private slots:
private:
QLabel *statusLabel = nullptr;
QPushButton *startButton = nullptr;
- QUdpSocket udpSocket;
+ QUdpSocket udpSocket4;
+ QUdpSocket udpSocket6;
QTimer timer;
- QHostAddress groupAddress;
+ QHostAddress groupAddress4;
+ QHostAddress groupAddress6;
int messageNo = 1;
};