如何在C#中找到客户端公共Internet IP地址? [英] How to find the client public internet IP address in C#?

查看:96
本文介绍了如何在C#中找到客户端公共Internet IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望客户端公共ip在c#我已经使用下面的代码获取客户端的公共IP但代码只获得托管服务器IP只是没有得到客户端的公共IP如何获取公共IP的客户。





这些代码只获得托管服务器ip。





我尝试过:



静态字符串GetIPAddress( )
{
String address =;
WebRequest request = WebRequest.Create(http://checkip.dyndns.org/);
using(WebResponse response = request.GetResponse())
using(StreamReader stream = new StreamReader(response.GetResponseStream()))
{
address = stream.ReadToEnd() ;
}

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

返回地址;
}





公共字符串GetIPAddress()
{
try
{
//字符串地址;
address =(new
WebClient())。DownloadString(http://checkip.dyndns.org/);
address =(新的正则表达式(@\d {1,3} \.\d {1,3} \.\d {1,3} \.\d {1 ,3)))
。匹配(地址)[0] .ToString();
返回地址;
}
catch {return null; }
}

解决方案

在您的服务器上执行对 CheckIP 服务的请求。在这种情况下,您的服务器是客户端,其IP地址将被返回。



另请注意,此类服务仅在执行网络翻译的路由器。否则,IP与本地网络接口的IP相同。



如果要获取服务器上已连接客户端的IP地址,只需获取该信息来自使用 Socket.RemoteEndPoint Property的套接字(System.Net.Sockets) [ ^ ]。


Dns.GetHostEntry Method(String)(System.Net) [ ^ ]


快速谷歌将透露这是一个非常常见的问题,答案永远不会改变......由于互联网的方式,你想做的事情是不可能的兽人。

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.



these code getting only hosted server ip.



What I have tried:

static string GetIPAddress()  
{  
    String address = "";  
    WebRequest request = WebRequest.Create("http://checkip.dyndns.org/");  
    using (WebResponse response = request.GetResponse())  
     using (StreamReader stream = new StreamReader(response.GetResponseStream()))  
     {  
        address = stream.ReadToEnd();  
     }  
  
     int first = address.IndexOf("Address: ") + 9;  
     int last = address.LastIndexOf("</body>");  
     address = address.Substring(first, last - first);  
  
     return address;  
}  



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; }
}

解决方案

The request to the CheckIP service is executed on your server. In that case your server is the client for which the IP address is returned.

Note also that such services are only useful when behind a router that performs network translations. Otherwise, the IP is the same as for the local network interface.

If you want to get the IP address of a connected client on a server, just get that information from the socket using the Socket.RemoteEndPoint Property (System.Net.Sockets)[^].


Dns.GetHostEntry Method (String) (System.Net)[^]


A quick google will have revealed that this is a very frequently asked question and the answer never changes...what you want to do isn't possible due to the way the internet works.


这篇关于如何在C#中找到客户端公共Internet IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆