1

I am trying to use QHostInfo or QDnsLookUp to look up a hostname and get a list of QHostAddress. I would prefer QHostInfo (the interface is simpler) but I tried also QDnsLookUp.

In the first case, I use QHostInfo::lookupHost() static function, and then I get the addresses from the result with QHostInfo::addresses()

In the second case I use QDnsLookup::lookup(), with the type set to QDnsLookup::A (IPv4 address records) and I get the results with QDnsLookup::hostAddressRecords() (and I get the value of the QDnsHostAddressRecord elements).

Well, both methods work somehow, but I get only one result...in both cases it should be a list of results from the documentation...but my list contains only one element...

Is there some option or something else that I should set to get the complete list? What could have gone wrong?

n3mo
  • 663
  • 8
  • 23

2 Answers2

0

You need to store result in a list, some examples:

QString myClass::getBroadWiFiAddress()
{
    QString ipAddress;
    QNetworkInterface wifi;
    // Get WiFi interface
     QList<QNetworkInterface> interfceList = QNetworkInterface::allInterfaces();
     for (int i = 0; i < interfceList.size(); ++i)
     {

         if (interfceList.at(i).name().contains("wireless") && interfceList.at(0).isValid() && interfceList.at(i).IsUp)
         {
             //qDebug() << "Interfaces:" << i << interfceList.at(i).name() << " / " << interfceList.at(i).humanReadableName();
             wifi = interfceList.at(i);
            break;
         }

     }

    QList<QHostAddress> ipAddressesList = wifi.allAddresses();
    // use the first non-localhost IPv4 address
    for (int i = 0; i < ipAddressesList.size(); ++i) {
        if (ipAddressesList.at(i) != QHostAddress::LocalHost &&
            ipAddressesList.at(i).toIPv4Address() ) {
            ipAddress = ipAddressesList.at(i).toString();
            //qDebug() << "Using following IP Address:" << ipAddress;
            break;
        }
    }
    //qDebug() << "getBroadWiFiAddress" << ipAddress;
    return ipAddress;
}
Mohammad Kanan
  • 4,452
  • 10
  • 23
  • 47
  • Hi Mohammad, I do not understand your answer...You are not using QDnsLookUp, nor QHostInfo in your example...I obviously store them in a list, the Qt functions themselves give me a list, but that list contains only 1 result, that is my problem... – n3mo Jan 17 '18 at 16:48
  • @n3mo, because I think the methods you mentioned may not necessarily return the list you wish, are you sure all addresses you want back are associated with same hostname .. the code I provided gets a list of interfaces and a list of addresses for a particular interface .. then you check the other way back, as the hostname of intereset may be associated only with a single address .. you might check – Mohammad Kanan Jan 17 '18 at 16:59
  • Yes, I checked with `dig` in Linux, the hostname has something like 30-40 addresses :) – n3mo Jan 18 '18 at 07:54
0

With the help of Dig - Google Apps I found out that the QDnsLookup::A option was not the correct solution. I have to use QDnsLookup::ANY in order to have a complete list.

n3mo
  • 663
  • 8
  • 23