-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
udplistener.cpp
41 lines (32 loc) · 946 Bytes
/
udplistener.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include "udplistener.hpp"
UDPListener::UDPListener(QObject *parent) : QObject(parent)
{
_udpSocket = new QUdpSocket(this);
connect(_udpSocket, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
}
UDPListener::~UDPListener()
{
_udpSocket->close();
_udpSocket->deleteLater();
}
bool UDPListener::bind(const QHostAddress &iAddress, quint16 iPort)
{
return _udpSocket->bind(iAddress, iPort);
}
void UDPListener::unBind()
{
_udpSocket->close();
}
void UDPListener::onReadyRead()
{
// when data comes in
QByteArray mBuffer;
mBuffer.resize(static_cast<int>(_udpSocket->pendingDatagramSize()));
QHostAddress mSender;
quint16 mSenderPort;
_udpSocket->readDatagram(mBuffer.data(), mBuffer.size(), &mSender, &mSenderPort);
qDebug() << "Message from: " << mSender.toString();
qDebug() << "Message port: " << mSenderPort;
qDebug() << "Message: " << mBuffer;
emit keyReceived(mBuffer);
}