0

Edit: It looks like this code only returns internal IPs. Is there a way of finding my external IP using C#?

So I've been using the 'standard' method of obtaining your own IP address:

IPAddress Own;
IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
    if (ip.AddressFamily == AddressFamily.InterNetwork)
    {
        Own = ip;
    }
}

The problem is the address this gives me is somewhere in the UK, and I'm in Australia - and obviously when I ask google for my own IP address I get something different from what this code gives me.

So why is this code not working? As I see it, it's a bit of a strange way to use a DNS server to resolve my own hostname (which isn't a URL, it's just a computer name...), but I trust that it's the right way. What could be going wrong?

Migwell
  • 18,631
  • 21
  • 91
  • 160
  • My guess is that your getting your internal ip address and that it is the same as that UK external address. – Silvermind Nov 12 '13 at 11:40
  • Do you recognize the IP that 'Own' contains after this code runs? Is it a private or a public IP? What does www.whatismyip.com say? How do you determine the address is "in" another country? What are you actually trying to do with this code? – CodeCaster Nov 12 '13 at 11:41
  • Okay so it turns out it is returning an internal IP, but it was the *hamatchi* internal IP, which was a UK external IP. In this case, is it possible to get an external IP using C#? – Migwell Nov 12 '13 at 11:45
  • This has been answered here : http://stackoverflow.com/a/2353177 – ono2012 Jul 16 '14 at 11:42

1 Answers1

0

Do you have any static IP address for your system? Generally, we use dynamic IP (as DHCP client) or local IP address for the office.

Google can show the IP address of your ISP, instead of local system; if you are not using a static IP address.

You can use any IP for your local system, which might belong to multiple local networks of your country or abroad.

Your code seems perfectly fine, and it's showing you the local IP address. Please confirm with network configuration.

recnac
  • 3,744
  • 6
  • 24
  • 46
Avik Das
  • 101
  • 2
  • 8