-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackgroundbroadcastsender.cpp
More file actions
28 lines (25 loc) · 936 Bytes
/
backgroundbroadcastsender.cpp
File metadata and controls
28 lines (25 loc) · 936 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include "backgroundbroadcastsender.h"
#include <QtNetwork>
#include <QtCore>
#include <QNetworkInterface>
BackgroundBroadcastSender::BackgroundBroadcastSender(QWidget *parent) : QWidget(parent)
{
udpSocket = new QUdpSocket(this);
connect(&timer, &QTimer::timeout, this, &BackgroundBroadcastSender::broadcastDatagram);
BackgroundBroadcastSender::startBroadcasting();
}
void BackgroundBroadcastSender::startBroadcasting()
{
timer.start(5000);
}
void BackgroundBroadcastSender::broadcastDatagram()
{
QByteArray datagram;
const QHostAddress &localhost = QHostAddress(QHostAddress::LocalHost);
for (const QHostAddress &address: QNetworkInterface::allAddresses()) {
if (address.protocol() == QAbstractSocket::IPv4Protocol && address != localhost){
datagram = address.toString().toUtf8();
udpSocket->writeDatagram(datagram, QHostAddress::Broadcast, 45454);
}
};
}