Get host name from IP address

前端 未结 1 1031
死守一世寂寞
死守一世寂寞 2020-12-06 17:54

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


        
相关标签:
1条回答
  • 2020-12-06 18:06

    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();
    
    0 讨论(0)
提交回复
热议问题