Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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());
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
491 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...