commit
f010f6dbe4
5 changed files with 114 additions and 0 deletions
@ -0,0 +1,2 @@ |
|||
*.pro.user |
|||
Tesp_Script.cpp |
@ -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 |
@ -0,0 +1,16 @@ |
|||
#include <QCoreApplication> |
|||
#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(); |
|||
} |
@ -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; |
|||
} |
@ -0,0 +1,26 @@ |
|||
#ifndef UDP_Socket_H |
|||
#define UDP_Socket_H |
|||
|
|||
#include <QObject> |
|||
#include <QUdpSocket> |
|||
|
|||
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
|
Loading…
Reference in new issue