Results 1 to 11 of 11

Thread: How to find the local IP (LAN) address of the PC

  1. #1
    Join Date
    Mar 2018
    Posts
    12
    Thanks
    10
    Qt products
    Qt5
    Platforms
    Windows

    Default How to find the local IP (LAN) address of the PC

    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.
    Attached Images Attached Images
    Attached Files Attached Files

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,456
    Thanked 271 Times in 266 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to find the local IP (LAN) address of the PC

    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.
    Last edited by Lesiok; 29th April 2019 at 09:38.

  3. The following user says thank you to Lesiok for this useful post:

    andreaQt (29th April 2019)

  4. #3
    Join Date
    Mar 2018
    Posts
    12
    Thanks
    10
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to find the local IP (LAN) address of the PC

    Quote Originally Posted by Lesiok View Post
    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
    Attached Files Attached Files

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,396
    Thanks
    37
    Thanked 1,542 Times in 1,493 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to find the local IP (LAN) address of the PC

    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,
    _

  6. The following user says thank you to anda_skoa for this useful post:

    andreaQt (30th April 2019)

  7. #5
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,456
    Thanked 271 Times in 266 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to find the local IP (LAN) address of the PC

    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)

  8. The following user says thank you to Lesiok for this useful post:

    andreaQt (30th April 2019)

  9. #6
    Join Date
    Mar 2018
    Posts
    12
    Thanks
    10
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to find the local IP (LAN) address of the PC

    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.

  10. #7
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,456
    Thanked 271 Times in 266 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to find the local IP (LAN) address of the PC

    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.

  11. The following user says thank you to Lesiok for this useful post:

    andreaQt (2nd May 2019)

  12. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,396
    Thanks
    37
    Thanked 1,542 Times in 1,493 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to find the local IP (LAN) address of the PC

    This line

    Qt Code:
    1. QNetworkInterface eth1Ip = QNetworkInterface::interfaceFromName(netInterface. name());
    To copy to clipboard, switch view to plain text mode 

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

    Cheers,
    _

  13. The following user says thank you to anda_skoa for this useful post:

    andreaQt (2nd May 2019)

  14. #9
    Join Date
    Mar 2018
    Posts
    12
    Thanks
    10
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to find the local IP (LAN) address of the PC

    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
    Last edited by andreaQt; 2nd May 2019 at 08:44.

  15. #10
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,456
    Thanked 271 Times in 266 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to find the local IP (LAN) address of the PC

    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 :
    Qt Code:
    1. QHostAddress my_local_address;
    2. QList<QHostAddress> addresses = QNetworkInterface::allAddresses();
    3. for(int i = 0; i < addresses.size(); i++)
    4. {
    5. if(addresses.at(i).isInSubnet(QHostAddress::parseSubnet("10.0.0.0/8"))
    6. || addresses.at(i).isInSubnet(QHostAddress::parseSubnet("172.16.0.0/12"))
    7. || addresses.at(i).isInSubnet(QHostAddress::parseSubnet("192.168.0.0/16")))
    8. {
    9. my_local_address = addresses.at(i);//this is my local IP
    10. break;
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

  16. The following user says thank you to Lesiok for this useful post:

    andreaQt (8th May 2019)

  17. #11
    Join Date
    Mar 2018
    Posts
    12
    Thanks
    10
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to find the local IP (LAN) address of the PC

    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.
    Attached Files Attached Files
    Last edited by andreaQt; 13th May 2019 at 17:13.

Similar Threads

  1. Replies: 2
    Last Post: 8th September 2017, 16:51
  2. Cannot find my local Bluetooth module
    By Blitzor DDD in forum Qt Programming
    Replies: 4
    Last Post: 18th August 2016, 12:08
  3. how to set local machine IP Address?
    By waiter in forum Qt Programming
    Replies: 1
    Last Post: 3rd July 2012, 21:41
  4. How to get the ip address of the local machine??
    By Gokulnathvc in forum Newbie
    Replies: 2
    Last Post: 29th July 2011, 22:39
  5. IP address find
    By kpmsivachand in forum Qt Programming
    Replies: 6
    Last Post: 21st February 2009, 18:11

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.