Skip to content

Instantly share code, notes, and snippets.

@thammi
Created September 5, 2014 23:24
Show Gist options
  • Save thammi/a062aff125ac25150f1c to your computer and use it in GitHub Desktop.
Save thammi/a062aff125ac25150f1c to your computer and use it in GitHub Desktop.
Testing link-local multicast in Qt
#####################################################################
# Automatically generated by qmake (3.0) Sa. Sep. 6 01:08:04 2014
######################################################################
CONFIG += console
QT += network
TEMPLATE = app
TARGET = test
INCLUDEPATH += .
# replace filename or rename ...
SOURCES += test.cpp
#include <QUdpSocket>
#include <QNetworkInterface>
#include <QThread>
int main(int argc, char *args[]) {
// iterates over the interfaces and sends on each
while(true) {
QUdpSocket udp;
QByteArray data("hello world");
foreach(const QHostAddress& address, QNetworkInterface::allAddresses()) {
udp.bind(address, 0);
qDebug() << address;
if(address.protocol() == QAbstractSocket::IPv6Protocol) {
udp.writeDatagram(data, QHostAddress("FF02::1"), 4321);
} else if(address.protocol() == QAbstractSocket::IPv4Protocol) {
udp.writeDatagram(data, QHostAddress::Broadcast, 4321);
}
udp.close();
}
QThread::sleep(5);
}
return 0;
}
#include <QUdpSocket>
#include <QThread>
int main(int argc, char *args[]) {
// simple test
while(true) {
QUdpSocket udp;
qDebug() << udp.writeDatagram(QByteArray("hello world"), QHostAddress("FF02::1"), 4321);
qDebug() << udp.writeDatagram(QByteArray("hello world"), QHostAddress::Broadcast, 4321);
QThread::sleep(5);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment