从IP地址获取主机名 [英] Get host name from IP address
本文介绍了从IP地址获取主机名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我设法使用下面的代码获取了连接的客户端IP,但似乎无法获取主机名。
I have managed to get the connected clients IP with the code below but can't seem to get the hostname.
Globals.connectedIPAddress = "" + IPAddress.Parse(((
IPEndPoint)_client.Client.RemoteEndPoint).Address.ToString());
推荐答案
不是每个IP地址都有一个名称。但是,给定 IPAddress
,您可以使用 Dns.GetHostEntry
到 try 来解决它。还要注意,如果它是NAT路由器,那么您将获得路由器的IP地址,而不是其实际计算机。
Well, not every IP address has a name. However, given the IPAddress
you can use Dns.GetHostEntry
to try to resolve it. Also note that if it's being a NAT router, you'll be getting the router's IP address rather than their actual machine.
只是为了解决注释中的问题,我同意ToString / Parse / ToString没有意义:
And just to address the point in the comments, I agree that there's no point in ToString/Parse/ToString:
IPAddress address = ((IPEndPoint)_client.Client.RemoteEndPoint).Address;
Globals.connectedIPAddress = address.ToString();
这篇关于从IP地址获取主机名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文