0

I try to get my correct IP but I can't

I'm using this code:

{
    IPHostEntry host;
    string localIP = "?";
    host = Dns.GetHostEntry(Dns.GetHostName());
    foreach (IPAddress ip in host.AddressList)
    {
        if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
        {
            localIP = ip.ToString();
        }
    }
    return localIP;
}

But it does not work for me!

For example my real IP is 151.246.147.86 but with this code I get 192.168.1.2.

Note: I have 4 Network adapters and in DOC with IPConfig I see this:

Network adapter: Local Area Connection 6
Media state : Media disconnected

Network adapter: Local Area Connection 4
Media state : Media disconnected

Network adapter: Local Area Connection 3
IP Address: 10.10.255.222

Network adapter: Local Area Connection
IP Address: 192.168.1.2

Now I connect to net and using with "Local Area Connection"; the public IP of my WAN connection is 151.246.147.86. I want to get this public IP (151.246.147.86), how can I do that?

Note : I don't want (and I can't, since I am using a VPN) use third party websites to get my IP

Please help!

-------EDIT :-------------

Note : I using from VPN and my VPN IP (for example) is : 176.227.197.111. But the IP of my WAN is: 151.246.147.86 and i want to get this address.

Lorenzo Dematté
  • 7,638
  • 3
  • 37
  • 77
Sara Ho
  • 15
  • 1
  • 5
  • 2
    You can't get your own **external IP** by yourself. You have to ask a server on the internet for your IP. Example: Add this PHP-Script to your web-server `` so that you get something like [this](https://www.fnkr.net/ip/?raw). Now you can let your application get the content of that URL. – fnkr Mar 11 '13 at 08:09
  • My guess: you are using a router/modem, therefore 192.168.1.2 is your "real IP". Your modem will be on the same (private) network, and its public interface (WAN) will have a public IP. So you need to get the IP on the public interface of your modem/router. – Lorenzo Dematté Mar 11 '13 at 08:11
  • @fnkr actually, it MAY be possible, if the router supports something like UPnP. Sara, which kind of modem/router do you have? – Lorenzo Dematté Mar 11 '13 at 08:12
  • Also, an alternative is to traceroute a know external address (google?) The first hop will be your own router... – Lorenzo Dematté Mar 11 '13 at 08:13
  • @fnkr : OK , But if I using from VPN how can i get real IP !!!!! :| I can't !!! so , i want to find my real IP , How can i do ? – Sara Ho Mar 11 '13 at 08:13
  • @SaraHo, do you have a VPN? this change things, you should specify it in your question. It is an important detail, you need to edit it and be as specific as possible. Also, please do specify your network configuration, and use the correct terminology if possible ("real IP" does not mean much...) – Lorenzo Dematté Mar 11 '13 at 08:15
  • @dema80 can you help me and say to me how can i get IP from public interface of your modem ? :/ – Sara Ho Mar 11 '13 at 08:16
  • @SaraHo it depends on your mark and model; if it supports UPnP you can do it, if not... who knows? but if you are on a VPN, there may be other ways. For example, do you know another host on the VPN? A server maybe? – Lorenzo Dematté Mar 11 '13 at 08:18

4 Answers4

1

What you ask is not possible unless there is some way to query your router/modem/external-most endpoint for its WAN address. The only IP address your computer knows about is its own (internal IP).

Technical note: there is nothing non-"real" about the IP address 192.168.1.2 - this is your computer's address. It is simply local to your given internal network and all but useless to anything outside.


If your router supports uPnP, you will need to query GetExternalIPAddress (starting point Is there a UPnP Library for .NET (C# or VB.NET)?). However since uPnP is considered dangerous and many security-conscious users turn it off, I would not count on it being enabled.

Querying an external service will be your most reliable bet for getting your external IP, whether it is one you write, or a third party service (see also https://superuser.com/questions/420969/services-that-just-return-your-public-ip-as-text).

Community
  • 1
  • 1
lc.
  • 113,939
  • 20
  • 158
  • 187
1

You (probably) are using a router/modem, therefore 192.168.1.2 is your "real IP". Your modem/router will be on the same (private) network, and its public interface (WAN) will have a public IP. So you need to get the IP on the public interface of your modem/router.

How to get the WAN IP of your modem depends on your mark and model; if it supports UPnP you can probably do it, or maybe if it is an enterprise class router it may also support SNMP... You should specify your make and model.

Another way without using external sites: do a tracert to a known site (google?) the first hop will be your route. Also, if you are on a VPN, you may be able to use the same technique. For example, if you know another host on the VPN (A server maybe?) you can ping/tracert it and discover your router from there. I don't know if in this case you will obtain what you call "a real IP" (by the way, how do you know this IP in the first place? You may be able to obtain it in the same way, programmatically).

Another solution for your VPN-based scenario: you can use Windows to help you. Windows has some kind of VPN management (RAS) which may help you; I would suggest starting from here to understand the basics, and then look for a library/SDK to help you (a quick google returned http://dotras.codeplex.com/).

Lorenzo Dematté
  • 7,638
  • 3
  • 37
  • 77
  • Personally, I would really like to know the make and model of the router. What started as a simpler question, now seems more "enterprise" oriented (router, VPN...) If the router is a "serious" one (cisco?) and supports it, I would go with SNMP – Lorenzo Dematté Mar 11 '13 at 08:39
0

There are many methods and tutorials that teach you how to find an ip address,I use this tool http://www.ipgp.net/ to display information about any ip address, just enter any IP address into the box and you will get the country, city, region, ISP, street address and the satellite location map for every query.

Bogdan M.
  • 11
  • 1
-1

you're trying to get your IP address from your computer or a server? It will make a lot of difference. If you're trying to get your IP address from your computer/laptop use http://www.howtochangeipaddress.com. Your IP will pop up in front of you soon you enter the site.