tcplistener怎么获取到客户端的ip 啊?

来源:百度知道 编辑:UC知道 时间:2024/06/27 09:50:41
tcplistener.server.remoteendpoint.tostring()这个不好用。

TcpListener.AcceptSocket().RemoteEndPoint()这个有问题。
public void Start()
{
TcpListener tl = new TcpListener(IPAddress.Any, 12311);
tl.Start();
while (true)
{
WriteConsole("等待连接中...");
TcpClient tc = tl.AcceptTcpClient();
WriteConsole("有客户连接...");//我想这步获取IP!怎么做?
那tcpclient怎么获取ip的啊?

IPEndPoint ip = (IPEndPoint)tc.Client.RemoteEndPoint;

AdrFamily=ip.AddressFamily.ToString();//地址类型
IpStr=ip.Address.ToString();//IP地址点分表达方式
IpPort=ip.Port.ToString();//IP地址端口号

WriteConsole("等待连接中...");
TcpClient tc = tl.AcceptTcpClient();

WriteConsole(tc.Client.RemoteEndPoint.ToString());//获取客户端IP地址和端口号

WriteConsole("有客户连接...");//我想这步获取IP!怎么做?

TcpListener只负责监听,TcpClient才能取到远程客户端地址