From 33e436e330ab9a9a8a6a212d0aeb6727fc757d97 Mon Sep 17 00:00:00 2001 From: Denis Davydov Date: Fri, 11 May 2018 15:23:28 +0300 Subject: [PATCH] The function "sendPing" and "send" is added. --- src/network.cpp | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/src/network.cpp b/src/network.cpp index cc46b45..7c86cd6 100644 --- a/src/network.cpp +++ b/src/network.cpp @@ -1,6 +1,28 @@ -#include "network.h" - -network::network() -{ - -} +#include "network.h" + +class Network +{ + Network() + { + socket = new QUdpSocket(this); + socket->bind(QHostAddress::AnyIPv6, 6552); + connect(socket, SIGNAL(readyRead()), SLOT(read())); + } + void sendPing(QHostAddress ip) + { + json ping; + ping["action"] = "ping"; + QString str = ping.dump(); + send(str, ip); + } + void send(QString str, QHostAddress ip) + { + QByteArray data; + QDataStream out(&data, QIODevice::WriteOnly); + out << qint64(0); + out << str; + out.device()->seek(qint64(0)); + out << qint64(data.size() - sizeof(qint64)); + socket->writeDatagram(data, QHostAddress(ip), 6552); + } +}