c# - TcpListener TcpClient 获取 IP 地址

标签 c# tcp

我希望从

获取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 地址和端口)的方法是相同的。

  1. 获取服务器上的客户端IP地址:

    IPEndPoint remoteIpEndPoint = newclient.Client.RemoteEndPoint as IPEndPoint;
    Console.WriteLine("Client IP Address is: {0}", remoteIpEndPoint.Address);
    
  2. 在客户端获取服务器IP地址:

        IPEndPoint remoteIpEndPoint = ftpclient.Client.RemoteEndPoint as IPEndPoint;
        Console.WriteLine("FTP Server IP Address is: {0}", remoteIpEndPoint.Address);
    

关于c# - TcpListener TcpClient 获取 IP 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60953041/

相关文章:

c++ - 如何告诉 parent 该线程是使用 pthreads 在 C++ 中完成的?

c - 如何在 linux C 中保持非事件的 TCP 连接?

c# - 我需要端口转发吗?

python - 使用 pycurl 中断的 S3 上传

javascript - __doPostBack() 导致回发但不调用 aspx 页面中的按钮单击事件

c# - If Else 在 linq 中使用匿名对象

C# - 像在 Java 的 Swing 中一样布置表单

c# - 提取韩文和数字字符

c# - 天蓝色函数 : how to watch a Dropbox or OneDrive folder and sync a ftp location

node.js - TCP和HASH验证