Sara Ho
Sara Ho

Reputation: 15

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.

Upvotes: 0

Views: 3612

Answers (5)

Sunil Acharya
Sunil Acharya

Reputation: 1183

This code is useful for obtaining the system’s local IPv4 address, handling network addresses, and managing potential exceptions if no IPv4 address is available.

// Local Variable to store the value
string systmLocal = GetLocalIPAddress();


// Define Public Static Method to get Ipv4 Address
public static string GetLocalIPAddress()
{
    var host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (var ip in host.AddressList)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                {
                    return ip.ToString();
                }
            }
    throw new Exception("No network adapters with an IPv4 address in the system!");
}

// Include the assembly information
using System.Net;
using System.Net.Sockets;

This code defines a method to retrieve the local IPv4 address of the system. Here's a breakdown of the key components:

Local Variable (systmLocal):

The variable systmLocal stores the local IPv4 address of the system, which is obtained by calling the GetLocalIPAddress() method. GetLocalIPAddress() Method:

This method retrieves the local system's IPv4 address. It first gets the host entry for the system using Dns.GetHostEntry(Dns.GetHostName()), which provides details about the host (like its IP addresses). Then, it iterates through the AddressList and checks if any address is of the InterNetwork family (IPv4). If found, it returns the IPv4 address as a string. If no IPv4 address is found, an exception is thrown with the message: "No network adapters with an IPv4 address in the system!". Imports (using Directives):

The code uses System.Net and System.Net.Sockets namespaces to access networking classes like Dns and AddressFamily. This code is useful for obtaining the system’s local IPv4 address, handling network addresses, and managing potential exceptions if no IPv4 address is available.

Upvotes: 0

user3534488
user3534488

Reputation: 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.

Upvotes: -1

Bogdan M.
Bogdan M.

Reputation: 11

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.

Upvotes: 0

Lorenzo Dematté
Lorenzo Dematté

Reputation: 7829

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

Upvotes: 1

lc.
lc.

Reputation: 116448

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

Upvotes: 1

Related Questions