s20/main.cpp

82 lines
3.0 KiB
C++
Raw Normal View History

2015-01-11 00:17:06 +00:00
/*************************************************************************
* Copyright (C) 2015 by Andrius Štikonas <andrius@stikonas.eu> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>.*
*************************************************************************/
#include <iostream>
#include <set>
2015-01-11 00:17:06 +00:00
#include <QByteArray>
#include <QCoreApplication>
2015-01-11 00:17:06 +00:00
#include <QUdpSocket>
#include "socket.h"
2015-01-13 17:34:28 +00:00
void listSockets(std::vector<Socket> const &sockets);
2015-01-13 17:34:28 +00:00
QByteArray discover = QByteArray::fromHex("68 64 00 06 71 61");
2015-01-11 00:17:06 +00:00
int main(int argc, char *argv[])
{
QCoreApplication s20(argc, argv);
2015-01-11 00:17:06 +00:00
QUdpSocket *udpSocketSend = new QUdpSocket();
QUdpSocket *udpSocketGet = new QUdpSocket();
2015-01-11 00:17:06 +00:00
udpSocketSend->connectToHost(QHostAddress::Broadcast, 10000);
2015-01-13 17:34:28 +00:00
udpSocketGet->bind(QHostAddress::Any, 10000);
2015-01-11 00:17:06 +00:00
udpSocketSend->write(discover);
std::vector<Socket> sockets;
2015-01-13 17:34:28 +00:00
while (udpSocketGet->waitForReadyRead(1000)) // 1s
{
while (udpSocketGet->hasPendingDatagrams())
{
QByteArray datagramGet;
datagramGet.resize(udpSocketGet->pendingDatagramSize());
QHostAddress sender;
quint16 senderPort;
udpSocketGet->readDatagram(datagramGet.data(), datagramGet.size(), &sender, &senderPort);
if (datagramGet != discover)
2015-01-13 17:34:28 +00:00
{
bool duplicate = false;
for(std::vector<Socket>::const_iterator i = sockets.begin() ; i != sockets.end(); ++i)
{
if (i->ip == sender)
duplicate = true;
}
if(!duplicate)
{
Socket socket(sender, datagramGet);
sockets.push_back(socket);
}
2015-01-13 17:34:28 +00:00
}
}
2015-01-11 00:17:06 +00:00
}
2015-01-13 17:34:28 +00:00
listSockets(sockets);
return 0;
2015-01-11 00:17:06 +00:00
}
void listSockets(std::vector<Socket> const &sockets)
2015-01-11 00:17:06 +00:00
{
for (std::vector<Socket>::const_iterator i = sockets.begin() ; i != sockets.end(); ++i)
{
std::cout << "IP Address: " << i->ip.toString().toStdString() << "\t MAC Address: " << i->mac.toHex().toStdString() << "\t Powered: " << i->powered << std::endl;
}
2015-01-11 00:17:06 +00:00
}