#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() { qDebug() << "Udp socket deleted!" << "\n"; delete _socket; } void UdpSocket::sayHello() { sayMsg("Connected!"); } void UdpSocket::sayMsg(QString Msg) { QByteArray Data; Data.append(Msg); _socket->writeDatagram(Data, QHostAddress::LocalHost, _port); } void UdpSocket::logReceivedData(QHostAddress& addr, quint16& port, QByteArray& msg) { qDebug() << "Address : " << addr.toString(); qDebug() << "Port : " << port; qDebug() << "Content : " << msg << '\n'; } void UdpSocket::readyRead() { QByteArray buffer; buffer.resize(_socket->pendingDatagramSize()); QHostAddress sender_addr; quint16 sender_port; _socket->readDatagram(buffer.data(), buffer.size(), &sender_addr, &sender_port); logReceivedData(sender_addr, sender_port, buffer); }