c# - TcpListener TcpClient 获取 IPAddress
问题描述
我希望从中获取IPAddress
服务器端
TcpListener ftp_listener = new TcpListener(IPAddress.Any, ftpport);
newclient = listener.AcceptTcpClient();
请问如何找到newclient ipaddress
客户端
TcpClient ftpclient = new TcpClient();
ftpclient.Connect(ipAddress, ftpport);
如何找到ftpclient ipaddress
目前我正在使用
TcpClient ftpclient = new TcpClient();
//get IpAddress of Server
#pragma warning disable CS0618 // Type or member is obsolete
IPAddress ipAddress = Dns.Resolve("localhost").AddressList[0];
#pragma warning restore CS0618 // Type or member is obsolete
ftpclient.Connect(ipAddress, ftpport);// "192.168.1.160", ftpport);
有没有更好的办法 ...
谢谢
解决方案
对于服务器和客户端,获取远程端点(IP 地址和端口)的方法是相同的。
获取服务器上的客户端 IP 地址:
IPEndPoint remoteIpEndPoint = newclient.Client.RemoteEndPoint as IPEndPoint; Console.WriteLine("Client IP Address is: {0}", remoteIpEndPoint.Address);
获取客户端上的服务器 IP 地址:
IPEndPoint remoteIpEndPoint = ftpclient.Client.RemoteEndPoint as IPEndPoint; Console.WriteLine("FTP Server IP Address is: {0}", remoteIpEndPoint.Address);