1

Several computers are connected to one wireless router. I can create a WCF service on one computer and use it from another using the computer's name as expressed in Environment.MachineName on the service hosting computer. However, I can't seem to be able to discover that name from the other computers.

Some of the things I've tried: (These are just the pertinent parts.)

This:

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

And this:

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

And also this:

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

And:

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.

So, how do I get the computers' names?

I'm not interested in any 3rd party library. I am aware of http://www.codeproject.com/Articles/16113/Retreiving-a-list-of-network-computer-names-using but that code is from 2006. I'm hoping there is some managed way to do this by now. And according to Getting computer names from my network places - "Do not use DirectoryServices unless your sure of a domain environment".

Community
  • 1
  • 1
ispiro
  • 26,556
  • 38
  • 136
  • 291
  • http://stackoverflow.com/a/2383454/264607 – BlackICE Jan 13 '14 at 14:53
  • Please explain your actual problem. Finding all computers on a network is relatively hard, and usually not what you want to do. Do you know about [WCF Discovery](http://msdn.microsoft.com/en-us/library/dd456782(v=vs.110).aspx)? – CodeCaster Jan 13 '14 at 14:56
  • @CodeCaster Is WCF Discovery a way to tell other computers about a WCF service? If so - that's exactly what I'm looking for. I want one computer to be able to know what other computers are out there with services to be used. – ispiro Jan 13 '14 at 14:59
  • Yes, it is. :-) From that link: _"WCF services can announce their availability to the network using a multicast message or to a discovery proxy server. Client applications can search the network or a discovery proxy server to find services that meet a set of criteria"_. – CodeCaster Jan 13 '14 at 15:00
  • 1
    @BlackICE That looks (at least at first glance) as a good resource. Though a long one. I'll have to take a look at it. Thanks. – ispiro Jan 13 '14 at 15:00

1 Answers1

3

You may obtain a list of the IPs of all computers present at the local network (as replied by the ARP command) using this snippet:

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;

}

You may try to individually resolve each machine's name by calling Dns.GetHostByAddress(targetIP).

OnoSendai
  • 3,960
  • 2
  • 22
  • 46
  • This assumes the IP address of the machine you want to connect to is in the ARP cache, which it doesn't have to be. – CodeCaster Jan 13 '14 at 14:59
  • @CodeCaster You're right about the ARP cache, but according to the OP, 'several computers are connected to one wireless router'. I would consider that an extraneous case - or am I wrong? – OnoSendai Jan 13 '14 at 15:03
  • Thanks. I replaced `Itms[0]` with `Dns.GetHostByAddress(Itms[0]).HostName` and am getting the following error: `The requested name is valid, but no data of the requested type was found`. – ispiro Jan 13 '14 at 15:07
  • I don't know what the `ARP cache` is. How would I check for it? – ispiro Jan 13 '14 at 15:08
  • ARP stands for Address Resolution Protocol, @ispiro. Whenever a machine goes up and announces itself or a query for a machine is made, the machine name/IP/MAC address mapping info (among others) is stored on a cache. More about ARP: http://en.wikipedia.org/wiki/Address_Resolution_Protocol – OnoSendai Jan 13 '14 at 15:12