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. Getting the Ip address the from active NIC on the local computer
Forum Updated to NodeBB v4.3 + New Features

Getting the Ip address the from active NIC on the local computer

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 12.6k 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.
  • A Offline
    A Offline
    AntMan1010
    wrote on last edited by
    #1

    Hello all, I'm trying to figure out a way to get just the ip address from the active NIC on a local computer.

    I figured out how to narrow it down to just the ipv4 addresses but I'm also getting with it, the loopback, the actual address im looking for, some random one that im not sure where its from, and the default dhcp address.

    so far my code looks like this

    @void MainWindow::on_pushButton_clicked()
    {
    ui->textEdit->append(QHostInfo::localHostName());

    QList<QHostAddress> addresses = QNetworkInterface::allAddresses();
    QHostAddress address;
    
    foreach(address, addresses)
    {
        if(address.protocol() == QAbstractSocket::IPv4Protocol)
        {
            ui->textEdit->append(address.toString());
        }
    }
    

    } @

    and my output is this

    XXXXXX-PC
    169.254.234.188
    192.168.1.7
    192.168.1.5
    127.0.0.1

    any suggestion?

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tony
      wrote on last edited by
      #2

      Hi,

      well, you should query your network interfaces in a different way.

      @
      QListIterator< QNetworkInterface > interface(QNetworkInterface::allInterfaces());

      while (interface.hasNext())
      {
      QNetworkInterface intf(interface.next());

      if ((interface.flags() & QNetworkInterface::IsUp) &&
      (interface.flags() & QNetworkInterface::IsRunning) &&
      (interface.flags() & ~QNetworkInterface::IsLoopBack))
      {
      QListIterator< QNetworkAddressEntry > networkAddressEntry(QNetworkInterface::addressEntries());

      while (networkAddressEntry.hasNext())
      {
        QNetworkAddressEntry netEntry(networkAddressEntry.next());
      
        if (netEntry.protocol() == QAbstractSocket::IPv4Protocol)
          ui->textEdit->append(netEntry.ip().toString());
      }
      

      }
      }
      @

      Maybe there're errors in this code due to the fact that I've typed it right now, without a compiler, but I hope that the concept is clear.

      Tony.

      1 Reply Last reply
      0
      • L Offline
        L Offline
        Lord
        wrote on last edited by
        #3

        I hope this will help...

        http://lists.trolltech.com/pipermail/qt-interest/2009-August/011969.html

        Its similar to Antonio Di Monaco sample.

        1 Reply Last reply
        0
        • ? This user is from outside of this forum
          ? This user is from outside of this forum
          Guest
          wrote on last edited by
          #4

          hi rahul, can u convert it to a link

          1 Reply Last reply
          0
          • L Offline
            L Offline
            Lord
            wrote on last edited by
            #5

            "http://lists.trolltech.com/pipermail/qt-interest/2009-August/011969.html":http://lists.trolltech.com/pipermail/qt-interest/2009-August/011969.html

            Thanks chetankjain. Next time I will keep that in mind :)

            1 Reply Last reply
            0
            • A Offline
              A Offline
              AntMan1010
              wrote on last edited by
              #6

              Thanks guys for the help!!

              So basically i should query the interfaces instead of just the ip addresses and use the interface flags and checking the protocol to knock out the info i don't need.

              taking from the examples i did this and got what i was looking for

              @foreach(QNetworkInterface interface, QNetworkInterface::allInterfaces())
              {
              if(interface.flags().testFlag(QNetworkInterface::IsUp) &&
              (!interface.flags().testFlag(QNetworkInterface::IsLoopBack)))
              {
              foreach(QNetworkAddressEntry entry, interface.addressEntries())
              {
              if(entry.ip().protocol() == QAbstractSocket::IPv4Protocol)
              {
              ui->textEdit->append(entry.ip().toString());
              }
              }
              }
              }@

              Thank you guys so much! Ya the Best!

              1 Reply Last reply
              0
              • RakettenR Offline
                RakettenR Offline
                Raketten
                wrote on last edited by
                #7

                Hi all,
                thanks for the help, this code helped me a lot.

                Does anybody know how to determine, if local IP comes from DHCP or is a static bind?
                Does anybody know how to assign new binding method (dhcp/static)

                and if static
                Does anybody know how to assign ip, mask and standard gateway.

                portable code is great, but I can be happy with Fedora code....

                Thanks in advance.

                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