Added a function that recognizes your IPv6.

This commit is contained in:
Denis Davydov 2018-06-22 15:07:02 +03:00
parent d557b16c6a
commit fd20de0e38
2 changed files with 12 additions and 3 deletions

View File

@ -13,7 +13,6 @@ void Network::sendDatagram(QJsonObject j, QString s)
{ {
QHostAddress ip = QHostAddress(s); QHostAddress ip = QHostAddress(s);
QByteArray baDatagram; QByteArray baDatagram;
//QDataStream out(&baDatagram, QIODevice::WriteOnly);
QJsonDocument jbuff = QJsonDocument(j); QJsonDocument jbuff = QJsonDocument(j);
quint16 p = 6552; quint16 p = 6552;
baDatagram = jbuff.toJson(QJsonDocument::Compact); baDatagram = jbuff.toJson(QJsonDocument::Compact);
@ -30,7 +29,15 @@ void Network::processTheDatagram()
QJsonDocument jbuff = QJsonDocument::fromJson(baDatagram); QJsonDocument jbuff = QJsonDocument::fromJson(baDatagram);
QJsonObject j = QJsonObject(jbuff.object()); QJsonObject j = QJsonObject(jbuff.object());
//QDataStream in(&baDatagram, QIODevice::ReadOnly);
//in >> jbuff.fromBinaryData(baDatagram);
emit json_received(j); emit json_received(j);
} }
QString Network::local_ipv6()
{
QHostAddress address;
foreach (address, QNetworkInterface::allAddresses()) {
if (address.protocol() == QAbstractSocket::IPv6Protocol && address != QHostAddress(QHostAddress::LocalHost))
break;
}
return(address.toString());
}

View File

@ -3,6 +3,7 @@
#include <QtNetwork> #include <QtNetwork>
#include <QJsonObject> #include <QJsonObject>
#include <QJsonDocument> #include <QJsonDocument>
#include <QNetworkInterface>
class Network : public QObject class Network : public QObject
{ {
@ -12,6 +13,7 @@ class Network : public QObject
QUdpSocket* udpSocket; QUdpSocket* udpSocket;
public: public:
Network(bool is_server = true); Network(bool is_server = true);
static QString local_ipv6();
public slots: public slots:
void sendDatagram(QJsonObject j, QString s); void sendDatagram(QJsonObject j, QString s);
signals: signals: