2

Connecting to another computer via sockets (which I have somewhat succeeded at, yay me) involve typing in the IP Address of the host computer, which my professor deemed "not user friendly".

My previous program involved using a Server, which is now no longer needed due to the Socket connection stuff. The code I used back then was to list down the Network names, something my professor liked very much.

NetworkBrowser nb = new NetworkBrowser();
foreach (string pc in nb.getNetworkComputers())
{
   lstNet.Items.Add(pc);
}

Would it be possible to, say, use that code, and have the program retrieve the IP Address (IPv4, if possible) when the User selects a Computer name on the network? This would be done before any socket connection is made.

Or if that fails / is impossible, how would I list down the IP Addresses of the computers in a Listbox? I'm really not sure how to implement that ARPing thing I keep hearing about.

Unfortunately, my school runs only .NET 2.0, so I'm afraid my only option is C# Windows Forms, and no WCF or anything.

Many thanks to all and any who answer.

zack_falcon
  • 4,186
  • 20
  • 62
  • 108

1 Answers1

4

You can perform a DNS request to get the IP address:

IPAddress[] addresslist = Dns.GetHostAddresses(pc);
foreach (IPAddress address in addresslist)
{
   Console.WriteLine(address.ToString());
}

You will have to include System.Net (available in .NET 2.0)

Flanfl
  • 516
  • 8
  • 29
  • That worked! It does list down the IPv6 though, among other things. Anyway I can filter that to just IPv4? A length check perhaps? But anyway, thanks a lot for the answer! – zack_falcon Nov 28 '11 at 11:25
  • 1
    The best way is to use the AddressFamily property as explained here: [link](http://stackoverflow.com/questions/6668810/c-sharp-get-ipv4-ipaddress-only) – Flanfl Nov 28 '11 at 11:31