PDA

View Full Version : How to find the local IP (LAN) address of the PC



andreaQt
29th April 2019, 08:47
Please read all, and don't tell me: if you forced it you know it, my goal is to find the IP of LAN even if i don't forced it.

I use Qt version 5.12.0 (see Version.jpg).

I built this program/modify because i need to have an interface that show the incoming UDP message on a specific port, my trouble is that i can't find the IP of the LAN (that i've forced before, see LAN.jpg) if i'm connected to internet => the program that i've wrote show me the IP of the internet connection and if i'm not connected to interne the program show me the right IP of the LAN (this is done in "Test cath Info 1").
Using "Test catch Info 2" i can show all the current connection LAN + internet, ecc, i test it that it is right using ipconfig command.
Using "Test catch Info 3" i try to find it by using the procedure/class "QNetworkInterface::xxxxxxxxxxxxxxx" but with no result at the moment (see Test_3a.jpg & Test_3_b.jpg).

Tell me where i do the mistake, or an alternative solution.

Thank you.

Lesiok
29th April 2019, 09:29
What does it mean "i'm connected to internet" ?
I'm connected to internet and Your program shows correct interface data (that is, the address in the internal network).

PS.
QNetworkInterface::inetrfaceFromName looks for interface name not host name.

andreaQt
29th April 2019, 10:17
What does it mean "i'm connected to internet" ?
I'm connected to internet and Your program shows correct interface data (that is, the address in the internal network).

PS.
QNetworkInterface::inetrfaceFromName looks for interface name not host name.

Maybe the antivirus/firewall i think is not correct setted or i have a malaware(I have Kaspersky total security), because sometimes when i start the mine program the IP that showed is right & sometimes is wrong.
An other trouble:
I try to comunicate with an arduino MEGA with Ethernet shield (W5100) using the program UDPSender (or with the UDPRecivier), using Wireshark to monitoring the flow of comuncation, and this is what happening:
1) First i program the arduino with the file you can find in the official page:
a - From Arduino to UDPReciver the packet that i send is right knowledged;
b - From UDPSender to Arduino the packet that i send is missed.
2) If I perform communication from UDPSender to UDPReciever all is right:
3)By using Wireshark to monitoring the communication sometimes i can look the communication protocol in the show window and sometimes no. Only with the configuration like point 1-b
no-one information is showed in show-window.
Even i sopend the Kaspersky temporary by using the procedure that the antivirus have made on.
Thank You

I make you my apologise this is the file

anda_skoa
29th April 2019, 12:30
I would try an approach like this:

1) Get all interfaces using QNetworkInterface::allInterfaces()

2) For each check if it is an Ethernet interface (assuming this is what you mean with LAN) using QNetworkInterface::type()

3) Get the addresses associated with that interface using QNetworkInterface::addressEntries()

4) Get one of the IP addresses. Probably only one anyway

Cheers,
_

Lesiok
29th April 2019, 12:56
And remeber that local networks are :
10.0.0.0 - 10.255.255.255 (10/8 prefix)
172.16.0.0 - 172.31.255.255 (172.16/12 prefix)
192.168.0.0 - 192.168.255.255 (192.168/16 prefix)

andreaQt
30th April 2019, 11:22
Thank you for your suggestion, by following it i find this solution & this run correctly; for the trouble of last version i will investigate if is it a problem by settling the antivirus ('cause i think i have to discarge the load of it before the OS is load) or i don't know why? Maybe last day i take a mistake : i confused the see of IP-Internet with the IP-Virtualbox, baut the trouble no.
Now the solution:
//Test cath info 5
foreach (const QNetworkInterface &netInterface, QNetworkInterface::allInterfaces()) {
qDebug() << "------------------------------------------------------------------";
qDebug() << "Test 5 name : " << netInterface.name();
qDebug() << "Test 5 type : " << netInterface.type();
qDebug() << "Test 5 flags : " << netInterface.flags();
qDebug() << "Test 5 index : " << netInterface.index();
qDebug() << "Test 5 HWaddress : " << netInterface.hardwareAddress();
qDebug() << "Test 5 HumanName : " << netInterface.humanReadableName();
QNetworkInterface eth1Ip = QNetworkInterface::interfaceFromName(netInterface. name());
QList<QNetworkAddressEntry> entries = eth1Ip.addressEntries();
foreach (const QNetworkAddressEntry &entry, entries){
qDebug() << "Test 5 IP : " << entry.ip().toString();
}
//if (address.protocol() == QAbstractSocket::IPv4Protocol && address.isLoopback() == false)
if ((netInterface.type() == QNetworkInterface::Ethernet) &&
// !(netInterface.humanReadableName() == "VirtualBox Host-Only Network") // Solved but i think only for italian WIN-OS (for other languages you need to chenge the string)
(netInterface.humanReadableName() == "Connessione alla rete locale (LAN)") // Alternative solution
){
qDebug() << "************************************************** ********";
qDebug() << "*************** Thih is what you search ******************";
qDebug() << "************************************************** ********";
}
}
//****************************************

Please where i can find the button to close the thread to SOLVED.

Lesiok
30th April 2019, 11:54
Why are you so complicating the problem ? Just check if the address belongs to the appropriate pool. This can be easily checked by the method QHostAddress::isInSubnet.

anda_skoa
1st May 2019, 08:20
This line



QNetworkInterface eth1Ip = QNetworkInterface::interfaceFromName(netInterface. name());


looks unnecessary.
"netInterface" is already a QNetworkInterface object.

Cheers,
_

andreaQt
2nd May 2019, 08:32
Right i fix it:
QNetworkInterface eth1Ip = netInterface.interfaceFromName(netInterface.name() );

Added after 11 minutes:

I don't understand how to use it -> QHostAddress::isInSubnet. .
My goal is to identify the IP of the LAN everywhere my program is running, by reading the Help in Qt (Returns true if this IP is in the subnet described by the network prefix subnet and netmask netmask.), so i have no idea how to use it to reach my goal.
Please can you post an example?
Thank you

Lesiok
2nd May 2019, 10:36
As I wrote : local networks are :
10.0.0.0 - 10.255.255.255 (10/8 prefix)
172.16.0.0 - 172.31.255.255 (172.16/12 prefix)
192.168.0.0 - 192.168.255.255 (192.168/16 prefix)
So something like this :

QHostAddress my_local_address;
QList<QHostAddress> addresses = QNetworkInterface::allAddresses();
for(int i = 0; i < addresses.size(); i++)
{
if(addresses.at(i).isInSubnet(QHostAddress::parseS ubnet("10.0.0.0/8"))
|| addresses.at(i).isInSubnet(QHostAddress::parseSubn et("172.16.0.0/12"))
|| addresses.at(i).isInSubnet(QHostAddress::parseSubn et("192.168.0.0/16")))
{
my_local_address = addresses.at(i);//this is my local IP
break;
}
}

andreaQt
13th May 2019, 17:17
To anda_skoa & Lesiok: thank you i understand your suggestions, late i change my code (and i set right IP on my LAN).
But now i have this trouble about program UDPbroadcastreceiver:
i want to draw a circle (red or green) to tell to the user if the connection is established or not, for more details read the comment that i've write at the beginning of file receiver.cpp.
You can find the new file in *.zip.

can you help me for an other trouble (look at the end of the thread), thank you.