0

I am trying to get Network IP Address as http://checkip.dyndns.org/ returns.

but i am getting this result from follwoing code.

fe80::28d3:b8e8:caab:63b3%10

i want ip address in internet dot-integer format lilke 122.168.149.143

foreach (IPAddress ipa in Dns.GetHostAddresses(Dns.GetHostName()))
{
    if (ipa.AddressFamily == AddressFamily.InterNetwork)
    {
        textBox2.Text = ipa.ToString();
        break;
    }
}
Haider Ali Wajihi
  • 2,756
  • 7
  • 51
  • 82

2 Answers2

3

You are currently filtering by IPv6 addresses when you mean to be filtering by IPv4 addresses! This is why IPAddress.ToString() is returning the IP in colon-hexadecimal notation.

To filter by IPv4 addresses you will need to filter according to AddressFamily.InterNetwork instead:

if (ipa.AddressFamily == AddressFamily.InterNetwork)
{ }

It is my understanding that you would like to obtain your public address. The code you listed will return your private (local) address!

The Windows Operating system does not care about your public IP. The Operating System simply routes out to the defined gateway and doesn't worry about the details. The only way to resolve your public IP is to query some external system. You need external help.

The most reliable way to obtain your public address is to connect to an external web server that can resolve and output your public address in plain-text. You already listed a suitable service with your question. You can even take responsibility for the service by providing the service yourself. The PHP code to achieve this is very simple.

If your router supports UPnP (or SNMP) you could query your router, although, this might not work. This might suffice for YOUR machine but some routers do not support UPnP and security conscious users may very well have disabled UPnP due to security holes. See this SO question for a managed UpNp library.

I have read that you can use tracert to an established website (one you know will be online) and the first "hop" will be to your route. I have never tried this.

Community
  • 1
  • 1
User 12345678
  • 7,714
  • 2
  • 28
  • 46
  • is there not any workaround in C# winform application to obtain wan address, why should i use a third link in my s/w, because, i have no rights on that link, any time that web page may unavailable. we may not prefer to depend on it. – Haider Ali Wajihi Jun 29 '13 at 13:09
  • 1
    @HaiderAliWajihi I'm not sure that it is the *only way* but I think it is the most production-friendly approach. I think to obtain your public IP without an external-web server will require much more code and may be reliant on certain hardware. I updated my post to include a PHP script you can upload to your own web server so that you can take responsibility for the service. – User 12345678 Jun 29 '13 at 13:20
  • thnx for precious time, it helped me a lot. – Haider Ali Wajihi Jun 29 '13 at 13:23
  • still i am looking this workaround in C# winform directly, without depending on any external url/webpage/link. +1 for your answer, effort and your knowledge is appereciatable. – Haider Ali Wajihi Jul 01 '13 at 05:56
  • @HaiderAliWajihi I updated my answer to include point you in the direction of two alternative methods. I have also justified why you need an external service (maybe not a server). – User 12345678 Jul 01 '13 at 21:50
1

The dot-integer format can be used only for IP ver. 4 addresses.

In the code sample you even explicitly select only IP ver. 6 addresses.

Use AddressFamily.InterNetwork instead of AddressFamily.InterNetworkV6 to select IPv4 address, then ToString will format it in the way you expect.

Jan Dobkowski
  • 641
  • 3
  • 6
  • i have corrected my program as per your suggestion, but it returns my local ip. "192.168.1.4" – Haider Ali Wajihi Jun 29 '13 at 12:57
  • Actually `fe80::28d3:b8e8:caab:63b3%10` was also your local IP. To get the address from http://checkip.dyndns.org/ you have to read it through HTTP from http://checkip.dyndns.org/ (eg. using System.Net.WebClient). – Jan Dobkowski Jun 29 '13 at 13:02
  • i want to know what they do in that link, is it not possible to obtain wan address my self ? – Haider Ali Wajihi Jun 29 '13 at 13:13