【问题标题】:Get network computers' names获取网络计算机的名称
【发布时间】:2014-01-13 14:49:09
【问题描述】:

多台计算机连接到一个无线路由器。我可以在一台计算机上创建 WCF 服务,并在另一台计算机上使用该计算机的名称,如服务托管计算机上的 Environment.MachineName 所示。但是,我似乎无法从其他计算机中发现该名称。

我尝试过的一些事情:(这些只是相关的部分。)

这个:

Dns.GetHostName(); ... //(Just gives me this computer's name.)

还有这个:

PrincipalContext ctx = new PrincipalContext(ContextType.Domain) ... // "The server could not be contacted."

还有这个:

DirectorySearcher searcher = new DirectorySearcher("(objectCategory=computer)", new[] { "Name" }); 
SearchResultCollection SRC = searcher.FindAll(); ... // "The specified domain either does not exist or could not be contacted."

还有:

DirectoryEntry root = new DirectoryEntry("WinNT:");
foreach (DirectoryEntry dom in root.Children)
    foreach (DirectoryEntry entry in dom.Children)
        if (entry.Name != "Schema")
            result += entry.Name + "\r\n"; // https://stackoverflow.com/a/5581339/939213 returns nothing.

那么,如何获得计算机的名称?

我对任何第 3 方库都不感兴趣。我知道http://www.codeproject.com/Articles/16113/Retreiving-a-list-of-network-computer-names-using,但该代码来自2006年。我希望现在有一些管理方法可以做到这一点。并且根据Getting computer names from my network places - “除非您确定域环境,否则不要使用 DirectoryServices”。

【问题讨论】:

  • 请解释您的实际问题。查找网络上的所有计算机相对困难,而且通常不是您想要做的。你知道WCF Discovery吗?
  • @CodeCaster WCF Discovery 是一种告诉其他计算机有关 WCF 服务的方法吗?如果是这样 - 这正是我正在寻找的。我希望一台计算机能够知道还有哪些其他计算机在使用服务。
  • 是的。 :-) 从该链接:“WCF 服务可以使用多播消息或发现代理服务器向网络宣布它们的可用性。客户端应用程序可以搜索网络或发现代理服务器以查找满足一组标准”.
  • @BlackICE 这看起来(至少乍一看)是一个很好的资源。虽然很长。我得看看它。谢谢。

标签: c# .net wcf networking


【解决方案1】:

您可以使用此 sn-p 获取本地网络中所有计算机的 IP 列表(由 ARP 命令回复):

static List<string> GetARP()
{
    List<string> _ret = new List<string>();

    Process netUtility = new Process();
    netUtility.StartInfo.FileName = "arp.exe";
    netUtility.StartInfo.CreateNoWindow = true;
    netUtility.StartInfo.Arguments = "-a";
    netUtility.StartInfo.RedirectStandardOutput = true;
    netUtility.StartInfo.UseShellExecute = false;
    netUtility.StartInfo.RedirectStandardError = true;
    netUtility.Start();

    StreamReader streamReader = new StreamReader(netUtility.StandardOutput.BaseStream, netUtility.StandardOutput.CurrentEncoding);

    string line = "";
    while ((line = streamReader.ReadLine()) != null)
    {

        if (line.StartsWith("  "))
        {
            var Itms = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            if (Itms.Length == 3)
                _ret.Add(Itms[0]);
        }
    }

    streamReader.Close();

    return _ret;

}

您可以尝试通过调用Dns.GetHostByAddress(targetIP) 来单独解析每台机器的名称。

【讨论】:

  • 这假设您要连接的机器的 IP 地址在 ARP 缓存中,但不一定是。
  • @CodeCaster 您对 ARP 缓存是正​​确的,但根据 OP,“多台计算机连接到一个无线路由器”。我会认为这是一个无关紧要的案例 - 还是我错了?
  • 谢谢。我将Itms[0] 替换为Dns.GetHostByAddress(Itms[0]).HostName 并收到以下错误:The requested name is valid, but no data of the requested type was found
  • 我不知道ARP cache 是什么。我将如何检查它?
  • ARP 代表地址解析协议,@ispiro。每当机器启动并宣布自己或对机器进行查询时,机器名称/IP/MAC 地址映射信息(以及其他)都存储在缓存中。更多关于 ARP:en.wikipedia.org/wiki/Address_Resolution_Protocol
猜你喜欢
  • 1970-01-01
  • 2014-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-05
  • 1970-01-01
相关资源
最近更新 更多