-
Notifications
You must be signed in to change notification settings - Fork 1
/
udpmutlticastsever.cpp
49 lines (42 loc) · 1.17 KB
/
udpmutlticastsever.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
#include "udpmutlticastsever.h"
const int SEVUDPPORT = 9632;
const int CLIUDPPORT = 9631;
const char* REQUEST = "request";
const char* RESPONSE = "response";
const char* SENDSTART = "sendstart";
UDPMutlticastSever::UDPMutlticastSever(QObject *parent) :
QObject(parent),
port_(SEVUDPPORT)
{
QObject::connect(&this->udpsocket_,&QUdpSocket::readyRead,this,&UDPMutlticastSever::gotmsg);
}
void UDPMutlticastSever::gotmsg()
{
QHostAddress clientaddress;
quint16 port;
QByteArray data;
int size = this->udpsocket_.pendingDatagramSize();
data.resize(size);
this->udpsocket_.readDatagram(data.data(),size,&clientaddress,&port);
if(data == RESPONSE)
{
emit getclient(clientaddress);
}
}
void UDPMutlticastSever::bind(QHostAddress addr)
{
this->udpsocket_.bind(addr,this->port_);
}
void UDPMutlticastSever::scan()
{
QByteArray data(REQUEST);
this->udpsocket_.writeDatagram(data,QHostAddress::Broadcast,CLIUDPPORT);
}
void UDPMutlticastSever::sendfile(QList<QHostAddress> hosts)
{
QByteArray data(SENDSTART);
foreach(QHostAddress address,hosts)
{
this->udpsocket_.writeDatagram(data,address,CLIUDPPORT);
}
}