0

In my program I am getting the local machine's public IP address like this

public static IPAddress getIPAddress()
{
    IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());
    foreach (IPAddress addr in localIPs)
    {
        if (addr.AddressFamily == AddressFamily.InterNetwork)
        {
            return addr;
        }
    }
    return null;
}

and it worked fine where I live.

Right now I am at a friend's house, and I am connected to internet via Wi-Fi, and this code does not give me my external IP address, it has probably something to do with the router settings, but I am not very familiar with networks...

The router is TP-LINK, and I can access its settings like this enter image description here

By the way, the 8080 port is exactly the one I need, I only need to be able to access my public IP. How can I do it?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Sartheris Stormhammer
  • 2,534
  • 8
  • 37
  • 81

3 Answers3

3

You can make a request to a site like http://icanhazip.com/ to get your external IP address

var request = WebRequest.Create("http://ipv4.icanhazip.com/");
var response = request.GetResponse();
var dataStream = response.GetResponseStream();
var reader = new StreamReader (dataStream);
string myIPAddress = reader.ReadToEnd();

More info here: https://msdn.microsoft.com/en-us/library/456dfw4f%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

DLeh
  • 23,806
  • 16
  • 84
  • 128
2

There is no sure-fire way to do this that will work 100% of the time. However, there are two methods that will work most of the time.

The first is to use an external STUN server. It's relatively easy to add a configuration entry to your software to allow the user to change the STUN server if this server ever changes or goes down, they can choose another one.. there are many of them out there. A STUN server returns the users IP address back to the caller and is used by most VOIP devices.

The second is to use the built-in Universal Plug-n-Play framework (UPnP) to ask the router for its IP Address. This one, however, depends on the router supporting UPnP and it not being disabled. Additionally, UPnP may not work within a corporate network as there are several layers of routers and firewalls usually. Still, for home users this is typically a good option. There is a .NET based UPnP library here that utilizes the built-in COM based UPnP components in Windows:

http://managedupnp.codeplex.com/

I don't have any examples of how to implement this, but it supposedly has a good documentation library.

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
1

What you want is not possible. A router is a proxy of sorts, whereby all traffic must pass through it. The external IP address for all internal devices (including PCs, laptops, tablets, etc), will be the same--the internal IP address what is different. The only way for a device to know its external IP address is either to query the router (which might not be possible), or to query an external source. The port forwarding page you have in your post only shows the router where to redirect incoming traffic on that port, but it will tell the PC nothing as to what its external IP address is--because, generally, to the PC the external IP address is irrelevant.

Russ
  • 4,091
  • 21
  • 32
  • I need to set my code to be universal, right now it only works if its directly connected to the internet, if with Wi-Fi I have to access 3rd party website. What guarantee I have that this site will always be available for accessing? – Sartheris Stormhammer Apr 08 '15 at 20:18
  • 1
    You don't have any guarantee. – Russ Apr 08 '15 at 20:20