Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Finding out the own IP address
Forum Updated to NodeBB v4.3 + New Features

Finding out the own IP address

Scheduled Pinned Locked Moved General and Desktop
7 Posts 5 Posters 23.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    koahnig
    wrote on last edited by
    #1

    Hi there,

    I have, what I think should be, a minor problem. I like to find out the ip address of the machine my application is running. While consulting Qt's documentation I have found QHostInfo, which seemed suitable for my purpose.
    @
    QHostInfo info;
    QString qstr = info.localHostName();
    qDebug() << qstr << endl;
    QHostInfo info2 ( QHostInfo ::fromName ( qstr ) );
    QList <QHostAddress> hostaddr = info2.addresses();
    qDebug () << hostaddr << endl;
    @

    On my machine the code above delivers a list of 6 different ip addresses. Four of them seem to be ipv6 and two ipv4 addresses.
    Four of the addresses (two ipv4 and two ipv6) I could locate. An ipv4 address is the address assigned by the router to my machine. That is actually the address I am looking for. There is also an ipv6 address assigned to the same network adapter. So I can use the ipv6 alternatively, I guess.
    The other pair of ipv4, ipv6 is assigned to my VirtualBox, which is currently not in use. The remaining two ipv6 addresses I do not know where they belong.
    I assume that I can use only 2 of the addresses to communicate with my machine (the ipv4 or the ipv6 address, see above).
    So my basic question is, how do I know which of the addresses is the address I have to use?
    Is the first the choice as suggested "in the example":http://doc.qt.nokia.com/latest/qhostinfo.html#addresses ?

    Vote the answer(s) that helped you to solve your issue(s)

    1 Reply Last reply
    1
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      If you want to communicate with your local machine, using localhost (127.0.0.1 resp. ::1) could be a solution. Most daemons etc. listen on that adress(es) too.

      You could also have a look at the Qt Mobility module (you might need to install it manually). QNetworkSession::interface() seems what you need. I never used it, so this could be complete nonsense - please check first :-)

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        Take care that a machine may have anywhere between 0 to N IP addresses. There is no such thing as the IP address of a machine.

        1 Reply Last reply
        0
        • K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          Thanks to both of you for your replies.

          @Andre yes, there are certainly several ip addresses available on most machines. Presumably for every hw network adapter installed and possibly a couple of virtual ones.

          @Volker unfortunately, the localhost is no option. I need to know how to communicate from another computer to my machine.
          I followed your suggestion a bit. The good news is, it is not complete nonsense. The bad news is that it did not help yet :-(
          I obtain another QList<QHostAdress> with two more entries (the localhost in ipv4 and ipv6). At least the sequence is different :-) and it is not a step back.
          I think I need to analyse the entries a bit more.

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0
          • K Offline
            K Offline
            koahnig
            wrote on last edited by
            #5

            at Volker: the interface brought up a solution (see code section below).

            @
            QNetworkConfigurationManager mgr;
            QNetworkConfiguration nconfig = mgr.defaultConfiguration();
            QNetworkSession session ( nconfig );
            QNetworkInterface ninter = session.interface();
            // the next statement gives you a funny name on windows
            qDebug() << ninter.name() << endl;
            // this gives ip addresses in different sequence, but is is a static method anyhow
            // (did not see in first place)
            qDebug() << ninter.allAddresses() << endl;

            // this provides two ip addresses (1 ipv4 and 1 ipv6) at least on my machine
            QList<QNetworkAddressEntry> laddr = ninter.addressEntries();
            for ( QList<QNetworkAddressEntry> ::const_iterator it = laddr.begin(); it != laddr.end(); ++it ) 
            {
                qDebug() << it->ip() << endl;
            }
            

            @

            If this is really the solution I will find out in the future.

            Vote the answer(s) that helped you to solve your issue(s)

            1 Reply Last reply
            0
            • S Offline
              S Offline
              shismitt
              wrote on last edited by
              #6

              I tried the above solution and it failed for the below scenario:
              Connect to internet using PPP adapter through USB and create a wifi adhoc network so that other computers can connect to internet using your internet connection.

              The problem I was facing was that I started a tcpServer on one machine and I wanted to provide the hostIp and port number (Protocol IpV4) to the machines in the local network. So this is what I did:

              @QString getHostIp(){
              qWarning()<<FUNCTION;
              QHostInfo hostInfo = QHostInfo::fromName(QHostInfo::localHostName());
              QList<QHostAddress> hostNameLookupAddressList = hostInfo.addresses();
              QList<QHostAddress> interfaceAddressList = QNetworkInterface::allAddresses();
              qDebug()<<FUNCTION<<"hostName lookup addresses:"<<hostNameLookupAddressList;
              qDebug()<<FUNCTION<<"interface addresses:"<<interfaceAddressList;

              QString hostIpStr;
              foreach(QHostAddress addr, hostNameLookupAddressList){
                  if(addr.protocol() == QAbstractSocket::IPv4Protocol && interfaceAddressList.contains(addr)){
                      if(isLocalIp(addr)){
                          qDebug()<<__FUNCTION__<<addr<<" is local ip";
                          hostIpStr = addr.toString();
                          break;
                      }else if(isLinkLocalAddress(addr)){
                          qDebug()<<__FUNCTION__<<addr<<" is Link Local Address";
                          hostIpStr = addr.toString();
                      }
                  }
              }
              
              return hostIpStr;
              

              }

              /**
              Link Local address should be in the range
              169.254.1.0 to 169.254.254.255
              Refer :http://en.wikipedia.org/wiki/Private_network
              **/
              bool isLinkLocalAddress(QHostAddress addr){
              quint32 hostIpv4Addr = addr.toIPv4Address();
              quint32 rangeMin = QHostAddress("169.254.1.0").toIPv4Address();
              quint32 rangeMax = QHostAddress("169.254.254.255").toIPv4Address();
              if(hostIpv4Addr >= rangeMin && hostIpv4Addr <= rangeMax){
              return true;
              }else{
              return false;
              }
              }

              /**
              Refer: http://en.wikipedia.org/wiki/Private_network
              Private Ipv4 address ranges:

              1. 10.0.0.0 - 10.255.255.255
              2. 172.16.0.0 - 172.31.255.255
              3. 192.168.0.0 - 192.168.255.255

              */

              bool isLocalIp(QHostAddress addr){
              quint32 hostIpv4Addr = addr.toIPv4Address();
              quint32 range1Min = QHostAddress("10.0.0.0").toIPv4Address();
              quint32 range1Max = QHostAddress("10.255.255.255").toIPv4Address();

              quint32 range3Min = QHostAddress("172.16.0.0").toIPv4Address();
              quint32 range3Max = QHostAddress("172.31.255.255").toIPv4Address();
              
              quint32 range2Min = QHostAddress("192.168.0.0").toIPv4Address();
              quint32 range2Max = QHostAddress("192.168.255.255").toIPv4Address();
              
              if((hostIpv4Addr >= range1Min && hostIpv4Addr <= range1Max)
                      || (hostIpv4Addr >= range2Min && hostIpv4Addr <= range2Max)
                      || (hostIpv4Addr >= range3Min && hostIpv4Addr <= range3Max)){
                  return true;
              }else{
                  return false;
              }
              

              }
              @

              1 Reply Last reply
              0
              • Q Offline
                Q Offline
                QReza
                wrote on last edited by
                #7

                It's work :)

                @
                foreach (const QHostAddress &address, QNetworkInterface::allAddresses()) {
                if (address.protocol() == QAbstractSocket::IPv4Protocol && address != QHostAddress(QHostAddress::LocalHost)) {
                qDebug() << address.toString();
                }
                }
                @

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved