开发者

C#, how do i get an ip address, from a TcpClient?

开发者 https://www.devze.com 2023-03-22 01:51 出处:网络
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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消