siwymilek
siwymilek

Reputation: 815

Finding each other computers in the same local network?

I want to do application to sharing data between computers in the same local network. Every computer should be able see each others. (sth like AirDrop in iOS and OSX).

Which is the best way to do this?

Upvotes: 0

Views: 158

Answers (2)

Orkun Bekar
Orkun Bekar

Reputation: 1441

You can use that function. It gives you a list of IP addresses in same network:

public List<string> GetARPResult()
        {
            string localIPAddress = Dns.GetHostAddresses(Environment.MachineName)[1].ToString();
            Process p = null;
            string output = string.Empty;
            List<string> listIP = new List<string>();

            try
            {
                p = Process.Start(new ProcessStartInfo("arp", "-a")
                {
                    CreateNoWindow = true,
                    UseShellExecute = false,
                    RedirectStandardOutput = true
                });

                output = p.StandardOutput.ReadToEnd();

                List<string> listArp = output.Split(' ').ToList();

                for (int i = 0; i < listArp.Count; i++)
                {
                   if (listArp[i].StartsWith(localIPAddress.Remove(localIPAddress.LastIndexOf("."))))
                    {
                        listIP.Add(listArp[i]);
                    }
                }

                // Remove local IP from IP list
                listIP.RemoveAt(0);

                p.Close();
            }
            catch (Exception ex)
            {
                throw new Exception("IPInfo: Error Retrieving 'arp -a' Results", ex);
            }
            finally
            {
                if (p != null)
                {
                    p.Close();
                }
            }

            return listIP;
        }

Upvotes: 0

Rofgar
Rofgar

Reputation: 358

The thing you want is called network broadcasting. You can write an UDP sender/receiver pair, sender broadcasts a package that queries other peers in networks, and peers catch that package with their receiver, and respond to the sender, notifying the sender about their presence.

For more detailed information, consider reading about p2p networks and udp datagram sockets.

Upvotes: 1

Related Questions