【问题标题】:Finding each other computers in the same local network?在同一本地网络中查找其他计算机?
【发布时间】:2015-02-17 23:39:46
【问题描述】:

我想做一个应用程序来在同一本地网络中的计算机之间共享数据。每台计算机都应该能够互相看到。 (类似于 iOS 和 OSX 中的 AirDrop)。

最好的方法是什么?

【问题讨论】:

    标签: c# network-programming


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      您可以使用该功能。它为您提供同一网络中的 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;
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-04
        相关资源
        最近更新 更多