UDP Server is started

This commit is contained in:
Ollo 2023-08-14 13:53:34 +02:00
parent 3aace88c0b
commit 488a8ed5dd
4 changed files with 11 additions and 8 deletions

View File

@ -6,6 +6,7 @@ MainWindow::MainWindow(QWidget *parent)
, ui(new Ui::MainWindow) , ui(new Ui::MainWindow)
{ {
ui->setupUi(this); ui->setupUi(this);
this->server = new UdpLedServer ();
} }
MainWindow::~MainWindow() MainWindow::~MainWindow()

View File

@ -2,6 +2,7 @@
#define MAINWINDOW_H #define MAINWINDOW_H
#include <QMainWindow> #include <QMainWindow>
#include "udpserver.h"
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; } namespace Ui { class MainWindow; }
@ -17,5 +18,6 @@ public:
private: private:
Ui::MainWindow *ui; Ui::MainWindow *ui;
UdpLedServer *server;
}; };
#endif // MAINWINDOW_H #endif // MAINWINDOW_H

View File

@ -4,23 +4,23 @@
#define UDP_IMAGE_PORT 4242 #define UDP_IMAGE_PORT 4242
UdpServer::UdpServer(QObject *parent) UdpLedServer ::UdpLedServer (QObject *parent)
: QAbstractItemModel(parent) : QObject(parent)
{ {
initSocket(); initSocket();
} }
void UdpServer::initSocket() void UdpLedServer ::initSocket()
{ {
this->mUdpSocket = new QUdpSocket(this); this->mUdpSocket = new QUdpSocket(this);
this->mUdpSocket->bind(QHostAddress::LocalHost, UDP_IMAGE_PORT); this->mUdpSocket->bind(QHostAddress::LocalHost, UDP_IMAGE_PORT);
connect(this->mUdpSocket, &QUdpSocket::readyRead, connect(this->mUdpSocket, &QUdpSocket::readyRead,
this, &UdpServer::readPendingDatagrams); this, &UdpLedServer ::readPendingDatagrams);
} }
void UdpServer::readPendingDatagrams() void UdpLedServer ::readPendingDatagrams()
{ {
while (this->mUdpSocket->hasPendingDatagrams()) { while (this->mUdpSocket->hasPendingDatagrams()) {
QNetworkDatagram datagram = this->mUdpSocket->receiveDatagram(); QNetworkDatagram datagram = this->mUdpSocket->receiveDatagram();
@ -28,7 +28,7 @@ void UdpServer::readPendingDatagrams()
} }
} }
void UdpServer::processTheDatagram(QNetworkDatagram datagram) { void UdpLedServer::processTheDatagram(QNetworkDatagram datagram) {
if (datagram.isValid()) { if (datagram.isValid()) {
qDebug() << "Received datagram:" << datagram.data().size(); qDebug() << "Received datagram:" << datagram.data().size();
} }

View File

@ -4,12 +4,12 @@
#include <QAbstractItemModel> #include <QAbstractItemModel>
#include <QUdpSocket> #include <QUdpSocket>
class UdpServer : public QAbstractItemModel class UdpLedServer : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit UdpServer(QObject *parent = nullptr); explicit UdpLedServer (QObject *parent = nullptr);
private: private: