#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); qInfo() << "A Socket built" << "\n"; } /*******************************************************************************************/ UdpSocket::~UdpSocket() { qInfo() << "Udp socket deleted!" << "\n"; delete _socket; } /*******************************************************************************************/ void UdpSocket::sayHello() { sayMsg("Connected!"); } /*******************************************************************************************/ void UdpSocket::sayMsg(const QString& msg) { QByteArray Data; Data.append(msg); _socket->writeDatagram(Data, QHostAddress::LocalHost, _port); } /*******************************************************************************************/ void UdpSocket::logReceivedData(const QHostAddress& addr, quint16 port, const QByteArray& msg) const { 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); }