如何确定连接套接字的远程IP地址?
我有一个我可以访问的RemoteEndPoint对象以及它的AddressFamily成员.
如何利用这些来查找IP地址?
谢谢!
目前正在尝试
IPAddress.Parse( testSocket.Address.Address.ToString() ).ToString();
并获得1.0.0.127而不是127.0.0.1的localhost端点.这是正常的吗?
http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.remoteendpoint.aspx
然后,您可以调用IPEndPoint .. ::.Address方法来检索远程IPAddress,并使用IPEndPoint .. ::.Port方法来检索远程端口号.
更多来自链接(固定很多嘿):
Socket s; IPEndPoint remoteIpEndPoint = s.RemoteEndPoint as IPEndPoint; IPEndPoint localIpEndPoint = s.LocalEndPoint as IPEndPoint; if (remoteIpEndPoint != null) { // Using the RemoteEndPoint property. Console.WriteLine("I am connected to " + remoteIpEndPoint.Address + "on port number " + remoteIpEndPoint.Port); } if (localIpEndPoint != null) { // Using the LocalEndPoint property. Console.WriteLine("My local IpAddress is :" + localIpEndPoint.Address + "I am connected on port number " + localIpEndPoint.Port); }
RemoteEndPoint是一个属性,其类型是System.Net.EndPoint,它继承自System.Net.IPEndPoint.
如果你看看IPEndPoint的成员,你会发现有一个Address
属性.