2009-01-03 6 views
6

새로운 TcpClient에서 기본 호스트 이름/포트를 검색 할 수 있습니까?System.Net.Sockets.TcpClient에서 호스트 이름과 포트를 검색 할 수 있습니까?

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

나는 client.Client (A System.Net.Socket)에서 주변에 전달했지만, 두 거기에 아무것도 찾을 수 없습니다. 어떤 아이디어?

감사합니다.

답변

13

테스트되지 않은,하지만 난 다음을 시도합니다 : 당신이하여 IPAddress와 함께 할 수있는 경우

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; 

, 내가 역 DNS-조회를 건너 뛸 것입니다,하지만 당신은 특별히 호스트 이름에 대해 물었다.

관련 문제