-
Notifications
You must be signed in to change notification settings - Fork 0
/
async.cpp
44 lines (36 loc) · 1.08 KB
/
async.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
#include "async.h"
#include <iostream>
Async::Async(QWidget *parent)
: QDialog(parent)
{
}
void Async::Receive(QString address, quint16 port)
{
udpSocket = new QUdpSocket(this);
udpSocket->bind(QHostAddress(address),port, QUdpSocket::ShareAddress);
connect(udpSocket, SIGNAL(readyRead()),this,SLOT(processPendingDatagrams()));
port_1 = port;
}
void Async::processPendingDatagrams()
{
QString OInMessage;
QHostAddress PeerIP;
while (udpSocket->hasPendingDatagrams())
{
QByteArray datagram;
datagram.resize(udpSocket->pendingDatagramSize());
udpSocket->readDatagram(datagram.data(),datagram.size(),&PeerIP,&port_1);
OInMessage=datagram.data();
char hello[1024];
strcpy(hello,datagram.data());
memset(hello,0,1024);
std::string message = std::string(hello, datagram.size());
update.update(message);
}
}
void Async::Send(QByteArray message,QString address, quint16 port)
{
udpSocket = new QUdpSocket(this);
QByteArray datagram = message;
udpSocket->writeDatagram(datagram.data(), datagram.size(), QHostAddress(address), port);
}