From f010f6dbe4747b58256fb14f313baf6d8d7f467f Mon Sep 17 00:00:00 2001 From: Hatef Date: Sun, 26 Jun 2022 13:37:38 +0430 Subject: [PATCH] Primitive UDP Protocole --- .gitignore | 2 ++ UDP.pro | 36 ++++++++++++++++++++++++++++++++++++ main.cpp | 16 ++++++++++++++++ socket.cpp | 34 ++++++++++++++++++++++++++++++++++ socket.h | 26 ++++++++++++++++++++++++++ 5 files changed, 114 insertions(+) create mode 100644 .gitignore create mode 100644 UDP.pro create mode 100644 main.cpp create mode 100644 socket.cpp create mode 100644 socket.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..178750e --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.pro.user +Tesp_Script.cpp diff --git a/UDP.pro b/UDP.pro new file mode 100644 index 0000000..ad5ec11 --- /dev/null +++ b/UDP.pro @@ -0,0 +1,36 @@ +QT += core +QT += network +QT -= gui + +TARGET = UDP +CONFIG += console +CONFIG -= app_bundle + +TEMPLATE = app + +CONFIG += c++11 console +CONFIG -= app_bundle + +# The following define makes your compiler emit warnings if you use +# any Qt feature that has been marked deprecated (the exact warnings +# depend on your compiler). Please consult the documentation of the +# deprecated API in order to know how to port your code away from it. +DEFINES += QT_DEPRECATED_WARNINGS + +# You can also make your code fail to compile if it uses deprecated APIs. +# In order to do so, uncomment the following line. +# You can also select to disable deprecated APIs only up to a certain version of Qt. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +SOURCES += \ + Tesp_Script.cpp \ + main.cpp \ + socket.cpp + +# Default rules for deployment. +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target + +HEADERS += \ + socket.h diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..103eebc --- /dev/null +++ b/main.cpp @@ -0,0 +1,16 @@ +#include +#include "socket.h" + +int main(int argc, char* argv[]) +{ + QCoreApplication a(argc, argv); + + UDP_Socket server; + UDP_Socket client; + + client.SayHello(); + server.SayHello(); + client.SayMsg("Msg"); + + return a.exec(); +} diff --git a/socket.cpp b/socket.cpp new file mode 100644 index 0000000..c55afdb --- /dev/null +++ b/socket.cpp @@ -0,0 +1,34 @@ +#include "socket.h" + +UDP_Socket::UDP_Socket(QObject* parent) : + QObject(parent) +{ + socket = new QUdpSocket(this); + socket->bind(QHostAddress::LocalHost, 1234); + connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead())); +} + +void UDP_Socket::SayHello() +{ + SayMsg("Connected :))"); +} + +void UDP_Socket::SayMsg(QString a) +{ + QByteArray Data; + Data.append(a); + socket->writeDatagram(Data, QHostAddress::LocalHost, 1234); +} + +void UDP_Socket::readyRead() +{ + QByteArray Buffer; + Buffer.resize(socket->pendingDatagramSize()); + QHostAddress sender_addr{}; + quint16 sender_port{}; + socket->readDatagram(Buffer.data(), Buffer.size(), &sender_addr, &sender_port); + + qDebug() << "Message from :" << sender_addr.toString(); + qDebug() << "Port : " << sender_port; + qDebug() << "Content : " << Buffer; +} diff --git a/socket.h b/socket.h new file mode 100644 index 0000000..7d0dc18 --- /dev/null +++ b/socket.h @@ -0,0 +1,26 @@ +#ifndef UDP_Socket_H +#define UDP_Socket_H + +#include +#include + +class UDP_Socket : public QObject +{ + Q_OBJECT + +public: + explicit UDP_Socket(QObject* parent = nullptr); + + void SayHello(); + void SayMsg(QString a); + +signals: + +public slots: + void readyRead(); + +private: + QUdpSocket* socket; +}; + +#endif //UDP_Socket_H