Raja

Raja

  • 784
  • 1.9k
  • 213k

How to find the client public internet IP Address in c#?

Jun 8 2018 5:56 AM
I want the client public ip in c# i have used the below code get the public ip of client but code is getting only hosted server IP only not getting client's public ip how to get the Public ip of client.
 
https://www.c-sharpcorner.com/blogs/how-to-get-public-ip-address-using-c-sharp1 
  1. static string GetIPAddress()
  2. {
  3. String address = "";
  4. WebRequest request = WebRequest.Create("http://checkip.dyndns.org/");
  5. using (WebResponse response = request.GetResponse())
  6. using (StreamReader stream = new StreamReader(response.GetResponseStream()))
  7. {
  8. address = stream.ReadToEnd();
  9. }

  10. int first = address.IndexOf("Address: ") + 9;
  11. int last = address.LastIndexOf("</body>");
  12. address = address.Substring(first, last - first);

  13. return address;
  14. }
  public string GetIPAddress()
{
try
{
// string address;
address = (new
WebClient()).DownloadString("http://checkip.dyndns.org/");
address = (new Regex(@"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"))
.Matches(address)[0].ToString();
return address;
}
catch { return null; }
}
 
these code getting only hosted server ip.
 
 

Brought to you by:

Answers (3)