DnsEndPoint
is just a .NET class that makes it possible to pass hostname/port endpoints to APIs that accept EndPoint
on input. It doesn't actually reflect how things work at the TCP protocol level.
A TCP connection is identified by a 5-tuple - a tuple with five elements:
- Protocol (TCP)
- Source IP address
- Source port
- Target IP address
- Target port
In order to establish a TCP connection, the target DnsEndPoint is resolved into an IP address. The host name is not transmitted to the target at all.
This can be observed with .NET's System.Net.Sockets.Socket
class as well:
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(new DnsEndPoint("test.rebex.net", 22));
Console.WriteLine(socket.RemoteEndPoint);
socket.Close();
The socket.RemoteEndPoint
returns an instance of IPEndPoint
even though the socket has been connected with DnsEndPoint
.
However, you can resolve an IP address into a hostname using DNS:
var ep = (IPEndPoint)args.ClientEndPoint;
IPHostEntry entry = Dns.GetHostEntry(ep.Address);
string hostName = (entry != null) ? entry.HostName : null;
But please note that:
- This actually involves querying the Domain Name System and might cause a noticeable delay.
- An IP address might not resolve into any hostname at all.