本文介绍了获取远程套接字端点的IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何确定连接套接字的远程IP地址?

How do I determine the remote IP Address of a connected socket?

我有一个RemoteEndPoint对象,我可以访问和以及它的AddressFamily成员。

I have a RemoteEndPoint object I can access and well as its AddressFamily member.

我如何利用这些来查找IP地址?

How do I utilize these to find the ip address?

谢谢!

目前试图

IPAddress.Parse( testSocket.Address.Address.ToString() ).ToString();

和越来越1.0.0.127,而不是127.0.0.1的本地主机终点。这是正常的吗?

and getting 1.0.0.127 instead of 127.0.0.1 for localhost end points. Is this normal?

推荐答案

http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.remoteendpoint.aspx

您可以调用的IPEndPoint .. ::。地址的方法来检索远程ip地址和IPEndPoint .. ::端口的方法来获取远程端口号。

You can then call the IPEndPoint..::.Address method to retrieve the remote IPAddress, and the IPEndPoint..::.Port method to retrieve the remote port number.

从链路(搞掂很多嘿嘿)更多:

More from the link (fixed up alot heh):

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);
}

这篇关于获取远程套接字端点的IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 12:28