0

I'm currently using the following function to get external IPv4 address of my client:

public static string GetExternalIP() { try { string externalIP; externalIP = (new WebClient()).DownloadString("http://checkip.dyndns.org/"); externalIP = (new Regex(@"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}")) .Matches(externalIP)[0].ToString(); return externalIP; } catch { return null; } } 

Is there any other way to get the same result without using any external website or services?

2
  • Are you sure that's the IP of your Client? Commented Aug 13, 2015 at 11:59
  • I have tested it with my own IP address and it was the same. But I'm also trying to get the internal IPv4 but I don't know how to do that? Commented Aug 13, 2015 at 12:00

2 Answers 2

2

GET INTERNAL IP ADDRESS

protected string GetInternalIP() { System.Web.HttpContext context = System.Web.HttpContext.Current; string ipAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; if (!string.IsNullOrEmpty(ipAddress)) { string[] addresses = ipAddress.Split(','); if (addresses.Length != 0) { return addresses[0]; } } return context.Request.ServerVariables["REMOTE_ADDR"]; } 

GET EXTERNAL IP ADDRESS

 public static string GetExternalIP() { try { string externalIP; externalIP = (new WebClient()).DownloadString("http://checkip.dyndns.org/"); externalIP = (new Regex(@"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}")) .Matches(externalIP)[0].ToString(); return externalIP; } catch { return null; } } 
Sign up to request clarification or add additional context in comments.

Comments

1

No. because actually only when you're connecting to a server he can tell you where you come from... so There is not any other option except making a connection to another service. its pretty fast tho... (if this what bothered you..) Hope you understood.

8 Comments

Thank you, I thought so. It's exactly because of the connection, sometimes it takes about 10+ seconds to get the response.
@KevinMaxwell yeah.. you know what i think Open.Nat actually does somthing similar.. i'll look it up for you..
@KevinMaxwell no.. they also request it from a service.. i just tried checkip.dyndns.org and it was really fast.. i think that's the most efficient and stable site for reteriving your ip.. by the way if you would like to not freeze the aplication while retreiving the Ip adress-just use it on a thread or use async method.. notify me if you need some help with that. Good luck
I think you are reading my mind. I actually want to put a "Loader" while it's fetching the IP address.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.