I cant seem to get or find information on finding my routers public IP? Is this because it cant be done this way and would have to get it from a website?
checkip.dyndns.org is not always works correctly. For example, for my machine it shows internal after-NAT address:
Current IP Address: 192.168.1.120
I think its happening, because of I have my local DNS-zone behind NAT, and my browser sends to checkip its local IP address, which is returned back.
Also, http is heavy weight and text oriented TCP-based protocol, so not very suitable for quick and efficient regular request for external IP address. I suggest to use UDP-based, binary STUN, especially designed for this purposes:
http://en.wikipedia.org/wiki/STUN
STUN-server is like "UDP mirror". You looking to it, and see "how I looks".
There is many public STUN-servers over the world, where you can request your external IP. For example, see here:
http://www.voip-info.org/wiki/view/STUN
You can download any STUN-client library, from Internet, for example, here:
http://www.codeproject.com/Articles/18492/STUN-Client
And use it.
string pubIp = new System.Net.WebClient().DownloadString("https://api.ipify.org");
Expanding on this answer by @suneel ranga:
static System.Net.IPAddress GetPublicIp(string serviceUrl = "https://ipinfo.io/ip")
{
return System.Net.IPAddress.Parse(new System.Net.WebClient().DownloadString(serviceUrl));
}
Where you would use a service with System.Net.WebClient that simply shows the IP address as a string and uses the System.Net.IPAddress object. Here are a few such services*:
* Some services were mentioned in this question and from these answers from superuser site.
Most of the answers have mentioned http://checkip.dyndns.org in solution. For us, it didn't worked out well. We have faced Timemouts a lot of time. Its really troubling if your program is dependent on the IP detection.
As a solution, we use the following method in one of our desktop applications:
// Returns external/public ip
protected string GetExternalIP()
{
try
{
using (MyWebClient client = new MyWebClient())
{
client.Headers["User-Agent"] =
"Mozilla/4.0 (Compatible; Windows NT 5.1; MSIE 6.0) " +
"(compatible; MSIE 6.0; Windows NT 5.1; " +
".NET CLR 1.1.4322; .NET CLR 2.0.50727)";
try
{
byte[] arr = client.DownloadData("http://checkip.amazonaws.com/");
string response = System.Text.Encoding.UTF8.GetString(arr);
return response.Trim();
}
catch (WebException ex)
{
// Reproduce timeout: http://checkip.amazonaws.com:81/
// trying with another site
try
{
byte[] arr = client.DownloadData("http://icanhazip.com/");
string response = System.Text.Encoding.UTF8.GetString(arr);
return response.Trim();
}
catch (WebException exc)
{ return "Undefined"; }
}
}
}
catch (Exception ex)
{
// TODO: Log trace
return "Undefined";
}
}
Good part is, both sites return IP in plain format. So string operations are avoided.
To check your logic in catch
clause, you can reproduce Timeout by hitting a non available port. eg: http://checkip.amazonaws.com:81/
Or this, it works quite well i think for what i needed. It's from here.
public IPAddress GetExternalIP()
{
WebClient lol = new WebClient();
string str = lol.DownloadString("http://www.ip-adress.com/");
string pattern = "<h2>My IP address is: (.+)</h2>"
MatchCollection matches1 = Regex.Matches(str, pattern);
string ip = matches1(0).ToString;
ip = ip.Remove(0, 21);
ip = ip.Replace("
", "");
ip = ip.Replace(" ", "");
return IPAddress.Parse(ip);
}
private static string GetPublicIpAddress()
{
using (var client = new WebClient())
{
return client.DownloadString("http://ifconfig.me").Replace("\n", "");
}
}