在同一局域网中查找彼此的计算机?

Finding each other computers in the same local network?

我想申请在同一个本地网络中的计算机之间共享数据。每台计算机都应该能够看到彼此。 (比如 iOS 和 OSX 中的 AirDrop)。

哪种方法最好?

您可以使用该功能。它为您提供同一网络中的 IP 地址列表:

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;
        }

你要的东西叫网络广播。 您可以编写一个 UDP sender/receiver 对,发送方广播一个包来查询网络中的其他对等方,对等方用它们的接收方捕获该包,并响应发送方,通知发送方它们的存在。

有关更多详细信息,请考虑阅读有关 p2p 网络和 udp 数据报套接字的内容。