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
@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.1any suggestion?
-
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.
-
I hope this will help...
http://lists.trolltech.com/pipermail/qt-interest/2009-August/011969.html
Its similar to Antonio Di Monaco sample.
-
"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 :)
-
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!
-
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.