简体   繁体   中英

How to get my correct IP address?

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.

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 ).

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/ ).

有很多方法和教程可以教您如何找到IP地址,我使用此工具http://www.ipgp.net/来显示有关任何IP地址的信息,只需在框中输入任何IP地址,您就会获得每个查询的国家,城市,区域,ISP,街道地址和卫星位置图。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM