Added minimal UDP client and server examples

This commit is contained in:
Tuomas Järvinen
2024-07-04 13:51:19 +03:00
parent f4027094f1
commit aae3e87116
6 changed files with 136 additions and 0 deletions
+39
View File
@@ -0,0 +1,39 @@
#include <QCoreApplication>
#include <QUdpSocket>
#include <QHostAddress>
#include <QThread>
#include <QJsonObject>
#include <QJsonDocument>
int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
if (argc != 2) {
qDebug() << "Give port number of server as only parameter";
return 1;
}
QUdpSocket udpSocket;
quint16 receiverPort = atoi(argv[1]);
for (int i = 0; i < 10000000 ; i++) {
QJsonObject json;
json["value"] = i;
QJsonDocument jsonDoc(json);
QByteArray datagram = jsonDoc.toJson();
qint64 bytesSent = udpSocket.writeDatagram(datagram, QHostAddress("127.0.0.1"), receiverPort);
if (bytesSent == -1) {
qWarning("Failed to send the datagram: %s", qPrintable(udpSocket.errorString()));
return 1;
}
else {
qDebug("Datagram sent successfully");
}
QThread::msleep(100);
}
return 0;
}
+6
View File
@@ -0,0 +1,6 @@
QT = core network
CONFIG += c++17 cmdline
SOURCES += \
main.cpp