Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query Local IP Address

I have the need to know my actual local IP address (i.e. not the loopback address) from a Windows 8 WinRT/Metro app. There are several reasons I need this. The simplest is that in the UI of the app I'd like to show some text like "Your local network IP address is: [IP queried from code]".

We also use the address for some additional network comms. Those comms are perfectly valid because it all works if I look at the IP address in the Control Panel, then hard-code it into the app. Asking the user in a dialog to go look at the address and manually enter it is something I really, really want to avoid.

I would think it wouldn't be a complex task to get the address programmatically, but my search engine and StackOverflow skills are coming up empty.

At this point I'm starting to consider doing a UDP broadcast/listen loop to hear my own request and extract the address from that, but that really seems like a hackey kludge. Is there an API somewhere in the new WinRT stuff that will get me there?

Note that I said "WinRT app. That means the typical mechanisms like Dns.GetHostEntry or NetworkInterface.GetAllInterfaces() aren't going to work.

like image 470
ctacke Avatar asked Apr 26 '12 15:04

ctacke


People also ask

What is the 127.0 0.1 address used for?

Localhost is the default name of the computer you are working on. The term is a pseudo name for 127.0. 0.1, the IP address of the local computer. This IP address allows the machine to connect to and communicate with itself.

How can I get localhost IP in PHP?

It is easy one. You can get the host name by this simple code. $ip = getHostByName(getHostName()); Or you can also use $_SERVER['HTTP_HOST'] to get the hostname.

How do I find local IP addresses on my network?

How do I identify an unknown device on my network? To see all of the devices connected to your network, type arp -a in a Command Prompt window. This will show you the allocated IP addresses and the MAC addresses of all connected devices.

How do I query my IP address?

One simple way to determine your computer's IP address is to visit whatsmyip.com. The site will display your IP address and other information about your system.


2 Answers

After much digging, I found the information you need using NetworkInformation and HostName.

NetworkInformation.GetInternetConnectionProfile retrieves the connection profile associated with the internet connection currently used by the local machine.

NetworkInformation.GetHostNames retrieves a list of host names. It's not obvious but this includes IPv4 and IPv6 addresses as strings.

Using this information we can get the IP address of the network adapter connected to the internet like this:

public string CurrentIPAddress() {     var icp = NetworkInformation.GetInternetConnectionProfile();      if (icp != null && icp.NetworkAdapter != null)     {         var hostname =             NetworkInformation.GetHostNames()                 .SingleOrDefault(                     hn =>                     hn.IPInformation != null && hn.IPInformation.NetworkAdapter != null                     && hn.IPInformation.NetworkAdapter.NetworkAdapterId                     == icp.NetworkAdapter.NetworkAdapterId);          if (hostname != null)         {             // the ip address             return hostname.CanonicalName;         }     }      return string.Empty; } 

Note that HostName has properties CanonicalName, DisplayName and RawName, but they all seem to return the same string.

We can also get addresses for multiple adapters with code similar to this:

private IEnumerable<string> GetCurrentIpAddresses() {     var profiles = NetworkInformation.GetConnectionProfiles().ToList();      // the Internet connection profile doesn't seem to be in the above list     profiles.Add(NetworkInformation.GetInternetConnectionProfile());      IEnumerable<HostName> hostnames =         NetworkInformation.GetHostNames().Where(h =>              h.IPInformation != null &&             h.IPInformation.NetworkAdapter != null).ToList();      return (from h in hostnames             from p in profiles             where h.IPInformation.NetworkAdapter.NetworkAdapterId ==                   p.NetworkAdapter.NetworkAdapterId             select string.Format("{0}, {1}", p.ProfileName, h.CanonicalName)).ToList(); } 
like image 126
Phil Avatar answered Sep 28 '22 20:09

Phil


About the accepted answer, you just need this:

HostName localHostName = NetworkInformation.GetHostNames().FirstOrDefault(h =>                     h.IPInformation != null &&                     h.IPInformation.NetworkAdapter != null); 

You can get the local IP Address this way:

string ipAddress = localHostName.RawName; //XXX.XXX.XXX.XXX 

Namespaces used:

using System.Linq; using Windows.Networking; using Windows.Networking.Connectivity; 
like image 31
Bruno Lemos Avatar answered Sep 28 '22 20:09

Bruno Lemos