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
and my output is this
well, you should query your network interfaces in a different way.
- while (interface.hasNext())
- QNetworkInterface intf(interface.next());
- if ((interface.flags() & QNetworkInterface::IsUp) &&
- (interface.flags() & QNetworkInterface::IsRunning) &&
- (interface.flags() & ~QNetworkInterface::IsLoopBack))
- while (networkAddressEntry.hasNext())
- QNetworkAddressEntry netEntry(networkAddressEntry.next());
- if (netEntry.protocol() == QAbstractSocket::IPv4Protocol)
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.
I hope this will help…
Its similar to Antonio Di Monaco sample.
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
- if(interface.flags().testFlag(QNetworkInterface::IsUp) &&
- foreach(QNetworkAddressEntry entry, interface.addressEntries())
- if(entry.ip().protocol() == QAbstractSocket::IPv4Protocol)
Thank you guys so much! Ya the Best!
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.