在.net remoting中查找远程对象的IP地址

发布于 2024-07-08 00:01:31 字数 257 浏览 1 评论 0原文

我有一个使用 .net 远程处理的客户端服务器应用程序。 客户端位于局域网中,我事先不知道他们的位置。

有时我们会进行文件传输,作为一种优化,我想确定客户端是否实际上与服务器位于同一台计算机上(这是很有可能的)。 在这种情况下,我只需要执行 File.Copy。

假设客户端调用远程方法:

RemoteFile server.GetFile(string path);

如何确定客户端(请求者)是否在同一台机器上?

I have a client-server application that uses .net remoting. The clients are in a LAN and i do not know their location in advance.

Sometimes we do file transfers and as an optimization I want to determine if a client is in fact on the same machine as the server (it is quite possible). In this case, I only need to do a File.Copy.

Let's say that a client calls the remote method:

RemoteFile server.GetFile(string path);

how can I determine if the client (the requester) is on the same machine?

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

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

发布评论

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

评论(1

ι不睡觉的鱼゛ 2024-07-15 00:01:31

如果您知道要调用远程方法的服务器的 IP 地址,则可以使用此方法来判断您是否在同一台计算机上:

using System.Net;

private bool CheckIfServer(IPAddress serverIP)
{
    // Get all addresses assigned to this machine
    List<IPAddress> ipAddresses = new List<IPAddress>();
    ipAddresses.AddRange(Dns.GetHostAddresses(Dns.GetHostName()));

    // If desirable, also include the loopback adapter
    ipAddresses.Add(IPAddress.Loopback);

    // Detect if this machine contains the IP for the remote server
    // Note: This uses a Lambda Expression, which is only available .Net 3 or later
    return ipAddresses.Exists(i => i.ToString() == serverIP.ToString());
}

如果您不知道远程服务器的 IP 地址,您可以轻松地使用服务器的主机名获取它,如下所示:

Dns.GetHostAddresses("remote_host_address")

这将返回一个 IPAddress[],其中包括该主机的所有已解析地址。

If you know the IP Address for the server you're calling the remote method from you can use this method to tell whether or not you're on the same machine:

using System.Net;

private bool CheckIfServer(IPAddress serverIP)
{
    // Get all addresses assigned to this machine
    List<IPAddress> ipAddresses = new List<IPAddress>();
    ipAddresses.AddRange(Dns.GetHostAddresses(Dns.GetHostName()));

    // If desirable, also include the loopback adapter
    ipAddresses.Add(IPAddress.Loopback);

    // Detect if this machine contains the IP for the remote server
    // Note: This uses a Lambda Expression, which is only available .Net 3 or later
    return ipAddresses.Exists(i => i.ToString() == serverIP.ToString());
}

If you don't know the IPAddress for your remote server you can easily get it using the server's host name like this:

Dns.GetHostAddresses("remote_host_address")

This returns an IPAddress[], which includes all the resolved address for that host.

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