获取远程套接字端点的 IP 地址

发布于 2024-08-15 00:40:51 字数 281 浏览 11 评论 0原文

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

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

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

谢谢!

目前正在尝试

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

为本地主机端点获取 1.0.0.127 而不是 127.0.0.1。这是正常的吗?

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

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

How do I utilize these to find the ip address?

Thanks!

Currently trying

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小忆控 2024-08-22 00:40:51

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

然后,您可以调用 IPEndPoint..::.Address 方法来检索远程 IPAddress,并调用 IPEndPoint..::.Port 方法来检索远程 IPAddress。检索远程端口号。

更多来自链接(修复了很多呵呵):

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 + " connected on port number " + localIpEndPoint.Port);
}

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

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 + " connected on port number " + localIpEndPoint.Port);
}
燃情 2024-08-22 00:40:51
string ip = ((IPEndPoint)(testsocket.RemoteEndPoint)).Address.ToString();
string ip = ((IPEndPoint)(testsocket.RemoteEndPoint)).Address.ToString();
要走就滚别墨迹 2024-08-22 00:40:51

RemoteEndPoint 是一个属性,其类型为 System.Net.EndPoint 继承自 System.Net.IPEndPoint

如果您查看 IPEndPoint 的成员,您'您会看到有一个 Address 属性。

RemoteEndPoint is a property, its type is System.Net.EndPoint which inherits from System.Net.IPEndPoint.

If you take a look at IPEndPoint's members, you'll see that there's an Address property.

星星的轨迹 2024-08-22 00:40:51

我在 VB.NET 中编写了这段代码,但你可以翻译。
假设您有变量 Client 作为 TcpClient

Dim ClientRemoteIP As String = Client.Client.RemoteEndPoint.ToString.Remove(Client.Client.RemoteEndPoint.ToString.IndexOf(":"))

希望它有帮助!干杯。

I've made this code in VB.NET but you can translate.
Well pretend you have the variable Client as a TcpClient

Dim ClientRemoteIP As String = Client.Client.RemoteEndPoint.ToString.Remove(Client.Client.RemoteEndPoint.ToString.IndexOf(":"))

Hope it helps! Cheers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文