-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommunicator.cpp
27 lines (25 loc) · 899 Bytes
/
communicator.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
#include "communicator.h"
Communicator::Communicator() {
socket = new QUdpSocket(this);
}
Communicator::Communicator(QHostAddress address, quint16 port) : Communicator() {
socket->bind(address, port);
connect(socket, SIGNAL(readyRead()), this, SLOT(slotReadyRead()));
}
void Communicator::sendToAddress(QString request, QHostAddress address, quint16 port) {
socket->writeDatagram(request.toUtf8(), address, port);
}
void Communicator::slotReadyRead()
{
QHostAddress sender;
quint16 senderPort;
QString response; // recieved request stringstream
qDebug() << "reading";
while (socket->hasPendingDatagrams()){
QByteArray datagram;
datagram.resize(socket->pendingDatagramSize());
socket->readDatagram(datagram.data(),datagram.size(),&sender,&senderPort);
response = QString(datagram);
}
emit messageRecieved(response);
}