-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
38 lines (32 loc) · 1.06 KB
/
Copy pathmainwindow.cpp
File metadata and controls
38 lines (32 loc) · 1.06 KB
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
29
30
31
32
33
34
35
36
37
38
#include "mainwindow.h"
#include <QVBoxLayout>
#include <QHostAddress>
#include <QXmlStreamReader>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
// GUI setup
textDisplay = new QTextEdit(this);
textDisplay->setReadOnly(true);
setCentralWidget(textDisplay);
setWindowTitle("UDP XML Receiver");
// UDP socket setup
socket = new QUdpSocket(this);
bool bound = socket->bind(QHostAddress::AnyIPv4, 50000, QUdpSocket::ShareAddress);
if (!bound) {
textDisplay->setText("Failed to bind to port 50000.");
return;
}
connect(socket, &QUdpSocket::readyRead, this, &MainWindow::processDatagrams);
}
MainWindow::~MainWindow() {}
void MainWindow::processDatagrams()
{
while (socket->hasPendingDatagrams()) {
QByteArray datagram;
datagram.resize(static_cast<int>(socket->pendingDatagramSize()));
socket->readDatagram(datagram.data(), datagram.size());
QString xmlString = QString::fromUtf8(datagram);
textDisplay->append("Received XML:\n" + xmlString + "\n---");
}
}