October 25, 2010

AntMan1010 AntMan1010
Lab Rat
1 posts

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

 

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

  1. void MainWindow::on_pushButton_clicked()
  2. {
  3.     ui->textEdit->append(QHostInfo::localHostName());
  4.    
  5.     QList<QHostAddress> addresses = QNetworkInterface::allAddresses();
  6.     QHostAddress address;
  7.  
  8.     foreach(address, addresses)
  9.     {
  10.         if(address.protocol() == QAbstractSocket::IPv4Protocol)
  11.         {
  12.             ui->textEdit->append(address.toString());
  13.         }
  14.     }
  15. }

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?

6 replies

October 25, 2010

Antonio Di Monaco Antonio Di Monaco
Lab Rat
116 posts

Hi,

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

  1. QListIterator< QNetworkInterface > interface(QNetworkInterface::allInterfaces());
  2.  
  3. while (interface.hasNext())
  4. {
  5.   QNetworkInterface intf(interface.next());
  6.  
  7.   if ((interface.flags() & QNetworkInterface::IsUp) &&
  8.       (interface.flags() & QNetworkInterface::IsRunning) &&
  9.       (interface.flags() & ~QNetworkInterface::IsLoopBack))
  10.   {
  11.     QListIterator< QNetworkAddressEntry > networkAddressEntry(QNetworkInterface::addressEntries());
  12.  
  13.     while (networkAddressEntry.hasNext())
  14.     {
  15.       QNetworkAddressEntry netEntry(networkAddressEntry.next());
  16.  
  17.       if (netEntry.protocol() == QAbstractSocket::IPv4Protocol)
  18.         ui->textEdit->append(netEntry.ip().toString());
  19.     }
  20.   }
  21. }

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.

October 25, 2010

Rahul Rahul
Lab Rat
14 posts

I hope this will help…

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

Its similar to Antonio Di Monaco sample.

October 25, 2010

Deleted Member # 4a2 Deleted Member # 4a2
Ant Farmer
1481 posts

hi rahul, can u convert it to a link

October 25, 2010

Rahul Rahul
Lab Rat
14 posts

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

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

October 26, 2010

AntMan1010 AntMan1010
Lab Rat
1 posts

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

  1. foreach(QNetworkInterface interface, QNetworkInterface::allInterfaces())
  2.     {
  3.         if(interface.flags().testFlag(QNetworkInterface::IsUp) &&
  4.            (!interface.flags().testFlag(QNetworkInterface::IsLoopBack)))
  5.         {
  6.             foreach(QNetworkAddressEntry entry, interface.addressEntries())
  7.             {
  8.                if(entry.ip().protocol() == QAbstractSocket::IPv4Protocol)
  9.                {
  10.                    ui->textEdit->append(entry.ip().toString());
  11.                }
  12.             }
  13.         }
  14.     }

Thank you guys so much! Ya the Best!

July 6, 2011

Raketten Raketten
Lab Rat
5 posts

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.

 
  ‹‹ QDialog not closing properly      QNetworkReply::isFinished() ››

You must log in to post a reply. Not a member yet? Register here!