-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudphelper.cpp
60 lines (50 loc) · 1.89 KB
/
udphelper.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "udphelper.h"
UDPHelper::UDPHelper(QObject *parent) :
QObject(parent)
{
// Create an QUDP socket
m_pSocket = new QUdpSocket(this);
m_pSocket->bind(QHostAddress::LocalHost, 1234);
connect(m_pSocket, SIGNAL(readyRead()),
this, SLOT(readPendingDatagrams()));
}
void UDPHelper::readPendingDatagrams()
{
while (m_pSocket->hasPendingDatagrams()) {
//Refer Qt documentation for available platform support.
//Some features of QNetworkDatagram are not supported in all operating systems.
//Only the address and ports of the remote host (sender in received packets and
//destination for outgoing packets) are supported in all systems.
//On most operating systems, the other features are supported only for IPv6.
//Software should check at runtime whether the rest could be determined for IPv4 addresses.
//TODO: send read data here?
//QNetworkDatagram datagram = m_pSocket->receiveDatagram();
emit dataReceived();
}
}
void UDPHelper::sendMessage(QString strMsg)
{
QByteArray Data;
Data.append(strMsg);
m_pSocket->writeDatagram(Data, QHostAddress::LocalHost, 1234);
}
QString UDPHelper::readData()
{
QByteArray buffer;
buffer.clear();
buffer.resize(m_pSocket->pendingDatagramSize());
QHostAddress sender;
quint16 senderPort;
m_pSocket->readDatagram(buffer.data(), buffer.size(),&sender, &senderPort);
// QString readString = "\nMessage from: "+ sender.toString()+ "\nMessage port: "+ senderPort + "\nMessage: " + buffer;
m_strIPAddress = sender.toString();
m_strPort = QString::number(senderPort);
// qDebug() << "Message from: " << sender.toString();
// qDebug() << "Message port: " << senderPort;
// qDebug() << "Message: " << buffer;
// if(buffer.isEmpty())
// {
// return "Connection established.";
// }
return buffer;
}