C#, how do i get an ip address, from a TcpClient?
I have a T开发者_Python百科cpClient and i want to get it's name.
Assuming you want the remote end point:
IPEndPoint ipep = (IPEndPoint)myTcpClient.RemoteEndPoint;
IPAddress ipa = ipep.Address;
Say you have a TcpClient
instance called MyTcpClient
.
private string IPAddress
{
get
{
IPEndPoint ep = MyTcpClient.Client.RemoteEndPoint as IPEndPoint;
if (ep == null)
return "unknown";
return ep.Address.ToString();
}
}
In case what you need is the local address instead you can use LocalEndPoint instead of RemoteEndPoimt in the previous replies.
精彩评论