2009-01-03 4 views
6

क्या एक नए टीसीपी क्लाइंट से अंतर्निहित होस्टनाम/पोर्ट पुनर्प्राप्त करना संभव है?क्या आप System.Net.Sockets.TcpClient से होस्टनाम और पोर्ट पुनर्प्राप्त कर सकते हैं?

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

मैं client.Client (एक System.Net.Socket) में चारों ओर से कराई है, लेकिन कुछ भी नहीं या तो वहाँ में पता कर सकते हैं। कोई विचार?

धन्यवाद सब कुछ।

उत्तर

13

untested है, लेकिन मैं निम्नलिखित की कोशिश करेंगे:

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; 

आप IPAddress के साथ क्या कर सकते हैं, मैं रिवर्स DNS-देखने को छोड़ जाएगा, लेकिन आप विशेष रूप से एक होस्ट नाम के लिए कहा।

संबंधित मुद्दे

 संबंधित मुद्दे