Can you get the host name and port from System.Net.Sockets.TcpClient?

Is it possible to get the base hostname / port from the new TcpClient?

TcpListener listener = new TcpListener(IPAddress.Any, port); TcpClient client = listener.AcceptTcpClient(); // get the hostname // get the port 

I headed to client.Client (a System.Net.Socket ) but can't find anything. Any ideas?

Thanks to everyone.

+6
c # networking
source share
1 answer

Unconfirmed, but I would try the following:

 TcpListener listener = new TcpListener(IPAddress.Any, port); TcpClient client = listener.AcceptTcpClient(); IPEndPoint endPoint = (IPEndPoint) client.Client.RemoteEndPoint; // .. or LocalEndPoint - depending on which end you want to identify IPAddress ipAddress = endPoint.Address; // get the hostname IPHostEntry hostEntry = Dns.GetHostEntry(ipAddress); string hostName = hostEntry.HostName; // get the port int port = endPoint.Port; 

If you can do with IPAddress, I would skip the reverse DNS lookup, but you specifically asked for the host name.

+13
source share

All Articles