2009-01-03 19 views
6

Temel ana makine adını/bağlantı noktasını yeni bir TcpClient'ten almak mümkün mü?Ana bilgisayar adını ve bağlantı noktasını System.Net.Sockets.TcpClient'ten alabilir misiniz?

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

Ben client.Client (a System.Net.Socket) etrafında yönlendirdim ama ya orada hiçbir şey bulamıyorum. Herhangi bir fikir?

Herkese teşekkürler.

cevap

13

Untested, ama şunları denemek istiyorum: Eğer IPAddress ile ne yapabiliyorsanız

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; 

, ben ters DNS-arama atlamak olurdu ama özellikle makine adı istedi.

İlgili konular