1

The question "How to get the IP address?" maybe seems topical and already answered in S.O., but I'm having troubles in the circunstances that I explain below.

I'm writting a simple helper class to provide miscellaneous networking utilities that will help me in my development. I would like to write a function that will return the Ipv4 or Ipv6 of the current machine, the same Ip that is used when I navigate through a web browser, you know... just the relevant Ip of the user, and I would like to do it without queryng to a random site to discover the Ip address that is used.

For most people the task would be simple as this:

(From ip As IPAddress In Dns.GetHostEntry(Dns.GetHostName).AddressList()
 Where ip.AddressFamily = AddressFamily.InterNetwork).FirstOrDefault

Or by iterating the System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces interfaces to do the same, however, I found that methodology is not efficient in most scenarios...

In my system I have 4 adapters, from those 4 adapters 2 are virtual adapters created by VMWare WorkStation (which are threated as Ethernet adapters), one virtual adapter from TAP-Win32 driver, and the "real" or physical Ethernet adapter that I use to navigate through Internet.

So, in C# or VB.Net, how can I Identify the proper adapter to retrieve the real Ip that is used to connect to Internet? the same Ip that returns websites like this?.

Note that when I ask "how to identify the proper adapter" what I'm asking is "how to identify the preferred adapter that the system will use to connect to Internet?", because an user could have many physical Ethernet adapters but only one will be the "preferred" to connect to Internet, then... how to do this?.

Community
  • 1
  • 1
ElektroStudios
  • 19,105
  • 33
  • 200
  • 417
  • 1
    Possible duplicate of [Get public/external IP address?](http://stackoverflow.com/questions/3253701/get-public-external-ip-address) – David Martin Nov 16 '15 at 11:51
  • 1
    The IP address used is highly likely to be different to that which is seen on the internet (hence the link to the duplicate question). If you need to identify the adapter then its likely to be the one with a gateway assigned, but as mentioned the IP address is not going to be the same as seen by internet sites. – David Martin Nov 16 '15 at 11:56
  • @David Martin Thanks for comment. From the 17 answers of the linked "duplicate" question, 15 of them does exactlly the same which consists in doing a query to a external site, except Toprak's solution that uses NATUPNPLib lib to do it, but for any reason I don't have that dll in my Windows filesystem, and kayabaku's solution didn't work, it throws a null reference exception, so unfortunately I still stuck in the same point. – ElektroStudios Nov 16 '15 at 12:10
  • In my case the Ip address that I get through .Net framework class library is the same as the Ip address that shows whatismyip.org and all those sort of sites, I don't look for a perfect perfection, just I would like to cover a common sceneario where the user has virtual adapters and/or more than one physical adapter, to avoid get a wrong Ip associated to one of those other adapters, instead of getting efficiently the same Ip as those "show me my ip" web services returns me. – ElektroStudios Nov 16 '15 at 12:17
  • you can use this http://stackoverflow.com/questions/23809385/how-to-get-unmanaged-variable-length-c-array-within-a-struct-from-c-to-c to get the route table and use the first result (which is the route for 0.0.0.0) –  Nov 16 '15 at 13:13
  • @Crisim Il Numenoreano Thanks for comment, but could you please give additional information? I don't understand what you mean about "use the first result", you mean to do a loop like in that example and use the first item of the loop iteration? but it is a IPFORWARDROW structure where I only see Integer values that does not corresponds to an Ip, what I'm missing? what is the thing you mean I should use and how to?. – ElektroStudios Nov 16 '15 at 15:21
  • the code will give you all the routes configured in your system. the dword values can be converted to ipv4 address with the right ctor. you can compare the result of the linked answer with a "route print" command, the results are in the same order –  Nov 16 '15 at 21:37
  • what are you REALLY trying to retrieve? the public ip or the ip of the adapter or the adapter itself? even reading all of your comments i cant understand.... –  Nov 18 '15 at 12:51
  • the public ip , thanks – ElektroStudios Nov 18 '15 at 14:20
  • the public ip of your intranet is obtained just like you did, an httprequest to an external site. if you need to know which adapter you used to make that request, just try the code in answer i linked before. if you need to know which adapter is used when you get called from the internet then good luck.... –  Nov 19 '15 at 13:03

1 Answers1

0

Here is a solution in vb.net using LINQ:

Function GetIP() As String
    Return (
        From networkInterface In networkInterface.GetAllNetworkInterfaces()
        Where networkInterface.NetworkInterfaceType = NetworkInterfaceType.Ethernet
        From address In networkInterface.GetIPProperties().UnicastAddresses
        Where address.Address.AddressFamily = Net.Sockets.AddressFamily.InterNetwork
        Select ip = address.Address.ToString()
    ).FirstOrDefault()
End Function    
shox
  • 677
  • 1
  • 5
  • 19