#include "socket.h" #include UDP_Socket::UDP_Socket(QObject* parent, quint16 port) : QObject(parent), the_port(port) { socket = new QUdpSocket(this); socket->bind(QHostAddress::LocalHost, the_port); connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead())); } void UDP_Socket::SayHello() { SayMsg("Connected :))"); } void UDP_Socket::SayMsg(QString Msg) { QByteArray Data; Data.append(Msg); socket->writeDatagram(Data, QHostAddress::LocalHost, the_port); } QString UDP_Socket::GetTime() { QDateTime current_time_object = QDateTime::currentDateTime(); QString formatted_time = current_time_object.toString("yyyy/MM/dd - hh:mm:ss"); return formatted_time; } 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() << "New Message arrived at" << GetTime(); qDebug() << "Address :" << sender_addr.toString(); qDebug() << "Port : " << sender_port; qDebug() << "Content : " << Buffer << '\n'; }