简体   繁体   中英

Get host name from IP address

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());

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.

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM