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;
    }
}

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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.
@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.
thnx for precious time, it helped me a lot.
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.
@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).
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.

3 Comments

i have corrected my program as per your suggestion, but it returns my local ip. "192.168.1.4"
Actually fe80::28d3:b8e8:caab:63b3%10 was also your local IP. To get the address from checkip.dyndns.org you have to read it through HTTP from checkip.dyndns.org (eg. using System.Net.WebClient).
i want to know what they do in that link, is it not possible to obtain wan address my self ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.