You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
838 B
35 lines
838 B
2 years ago
|
#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;
|
||
|
}
|