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.
45 lines
1.1 KiB
45 lines
1.1 KiB
#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);
|
|
}
|
|
|