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.

46 lines
1.1 KiB

#include "socket.h"
UdpSocket::UdpSocket(QObject* parent, quint16 port) :
QObject(parent),
_port(port)
{
_socket = new QUdpSocket(this);
_socket->bind(QHostAddress::LocalHost, port);
connect(_socket, &QUdpSocket::readyRead, this, &UdpSocket::readyRead);
}
UdpSocket::~UdpSocket()
{
2 years ago
qDebug() << "Udp socket deleted!" << "\n";
delete _socket;
}
void UdpSocket::sayHello()
{
2 years ago
sayMsg("Connected!");
}
void UdpSocket::sayMsg(QString Msg)
{
QByteArray Data;
Data.append(Msg);
_socket->writeDatagram(Data, QHostAddress::LocalHost, _port);
}
2 years ago
void UdpSocket::logReceivedData(QHostAddress& addr, quint16& port, QByteArray& msg)
{
2 years ago
qDebug() << "Address : " << addr.toString();
qDebug() << "Port : " << port;
qDebug() << "Content : " << msg << '\n';
}
void UdpSocket::readyRead()
{
2 years ago
QByteArray buffer;
buffer.resize(_socket->pendingDatagramSize());
QHostAddress sender_addr;
quint16 sender_port;
2 years ago
_socket->readDatagram(buffer.data(), buffer.size(), &sender_addr, &sender_port);
logReceivedData(sender_addr, sender_port, buffer);
}