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.
 
 

48 lines
1.4 KiB

#include "logger.h"
#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";
_socket->close();
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::readyRead()
{
while(_socket->hasPendingDatagrams())
{
QByteArray buffer;
buffer.resize(_socket->pendingDatagramSize());
QHostAddress sender_addr;
quint16 sender_port;
_socket->readDatagram(buffer.data(), buffer.size(), &sender_addr, &sender_port);
Logger::logReceivedData(buffer, sender_addr, sender_port);
}
}