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"
2 years ago
#include <QDateTime>
2 years ago
UDP_Socket::UDP_Socket(QObject* parent, quint16 port) :
QObject(parent),
the_port(port)
{
socket = new QUdpSocket(this);
2 years ago
socket->bind(QHostAddress::LocalHost, the_port);
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
}
void UDP_Socket::SayHello()
{
SayMsg("Connected :))");
}
2 years ago
void UDP_Socket::SayMsg(QString Msg)
{
QByteArray Data;
2 years ago
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);
2 years ago
qDebug() << "New Message arrived at" << GetTime();
qDebug() << "Address :" << sender_addr.toString();
qDebug() << "Port : " << sender_port;
2 years ago
qDebug() << "Content : " << Buffer << '\n';
}