From a2fee2fe264897dc395adf9d77a57ced69c526d7 Mon Sep 17 00:00:00 2001 From: Denis Davydov Date: Thu, 21 Jun 2018 18:23:36 +0300 Subject: [PATCH] Changed the Network class constructor Now sockets do not overlap (if you create an object of class Network in some other class). --- src/kernel/handler.cpp | 4 ++-- src/kernel/network.cpp | 9 ++++++--- src/kernel/network.hpp | 2 +- src/mainwindow.cpp | 4 ++-- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/kernel/handler.cpp b/src/kernel/handler.cpp index f1ec558..5b97ec3 100644 --- a/src/kernel/handler.cpp +++ b/src/kernel/handler.cpp @@ -2,7 +2,7 @@ Handler::Handler() { - network = new Network(); + network = new Network(false); connect(network, &Network::json_received, this, &Handler::handle); } @@ -12,7 +12,7 @@ void Handler::handle(QJsonObject jsonReceived) QJsonObject jsonSend; if(jsonReceived["action"] == "createSession" && !jsonReceived.contains("status")) { - jsonSend["peerID"] = QHostAddress("::1").toString(); + jsonSend["peerID"] = QHostAddress("fc8f:cc50:70b0:3731:d686:a75e:94f2:f44f").toString(); jsonSend["action"] = "createSession"; jsonSend["status"] = true; QString peerReceiver = jsonReceived["peerID"].toString(); diff --git a/src/kernel/network.cpp b/src/kernel/network.cpp index b36f0dc..26b37d1 100644 --- a/src/kernel/network.cpp +++ b/src/kernel/network.cpp @@ -1,9 +1,12 @@ #include "network.hpp" -Network::Network() +Network::Network(bool is_server) { udpSocket = new QUdpSocket(this); - udpSocket->bind(QHostAddress::AnyIPv6, 6552); - connect(udpSocket, SIGNAL(readyRead()), this, SLOT(processTheDatagram())); + if (is_server) + { + udpSocket->bind(QHostAddress::AnyIPv6, 6552); + connect(udpSocket, SIGNAL(readyRead()), this, SLOT(processTheDatagram())); + } } void Network::sendDatagram(QJsonObject j, QString s) diff --git a/src/kernel/network.hpp b/src/kernel/network.hpp index 1c265cb..5eb40bb 100644 --- a/src/kernel/network.hpp +++ b/src/kernel/network.hpp @@ -11,7 +11,7 @@ class Network : public QObject private: QUdpSocket* udpSocket; public: - Network(); + Network(bool is_server = true); public slots: void sendDatagram(QJsonObject j, QString s); signals: diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 312df74..0fd0310 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -6,7 +6,7 @@ MainWindow::MainWindow(QWidget *parent) : ui(new Ui::MainWindow) { ui->setupUi(this); - network = new Network(); + network = new Network(true); handler = new Handler(); } @@ -18,7 +18,7 @@ MainWindow::~MainWindow() void MainWindow::on_connectToPeer_clicked() { QJsonObject j; - j["peerID"] = QHostAddress("::1").toString(); + j["peerID"] = QHostAddress("fc8f:cc50:70b0:3731:d686:a75e:94f2:f44f").toString(); j["action"] = "createSession"; QString s = ui->peerID->text(); network->sendDatagram(j, s);