我正在开发一个家庭安全的应用程序。有一件事我想做的就是自动地把它关掉,并根据是否或不我是在家里。我有个手机与无线,自动连接到我的网络,当我回家。

电话连接和获取其地址的经验。同时我可以将它配置为使用静态IP,我宁愿没有。是否有任何种类的'平'或相当于在C#/.净,可以采取的MAC地址的设备,并告诉我它是否是当前活动的网络上的?

编辑: 为了澄清,我正在运行的软件的电脑上我想要有能够检测的手机上的同一个局域网。

编辑: 这里是代码,我想到了,谢谢spoulson的帮助。它可靠的检测是否有任何的电话我很感兴趣,是在房子里。

private bool PhonesInHouse()
{

    Ping p = new Ping();
    // My home network is 10.0.23.x, and the DHCP 
    // pool runs from 10.0.23.2 through 10.0.23.127.

    int baseaddr = 10;
    baseaddr <<= 8;
    baseaddr += 0;
    baseaddr <<= 8;
    baseaddr += 23;
    baseaddr <<= 8;

    // baseaddr is now the equivalent of 10.0.23.0 as an int.

    for(int i = 2; i<128; i++) {
        // ping every address in the DHCP pool, in a separate thread so 
        // that we can do them all at once
        IPAddress ip = new IPAddress(IPAddress.HostToNetworkOrder(baseaddr + i));
        Thread t = new Thread(() => 
             { try { Ping p = new Ping(); p.Send(ip, 1000); } catch { } });
        t.Start();
    }

    // Give all of the ping threads time to exit

    Thread.Sleep(1000);

    // We're going to parse the output of arp.exe to find out 
    // if any of the MACs we're interested in are online

    ProcessStartInfo psi = new ProcessStartInfo();
    psi.Arguments = "-a";
    psi.FileName = "arp.exe";
    psi.RedirectStandardOutput = true;
    psi.UseShellExecute = false;
    psi.CreateNoWindow = true;

    bool foundone = false;
    using (Process pro = Process.Start(psi))
    {
        using (StreamReader sr = pro.StandardOutput)
        {
            string s = sr.ReadLine();

            while (s != null)
            {
                if (s.Contains("Interface") || 
                    s.Trim() == "" || 
                    s.Contains("Address"))
                {
                    s = sr.ReadLine();
                    continue;
                }
                string[] parts = s.Split(new char[] { ' ' }, 
                    StringSplitOptions.RemoveEmptyEntries);

                // config.Phones is an array of strings, each with a MAC 
                // address in it.
                // It's up to you to format them in the same format as 
                // arp.exe
                foreach (string mac in config.Phones)
                {
                    if (mac.ToLower().Trim() == parts[1].Trim().ToLower())
                    {
                        try
                        {
                            Ping ping = new Ping();
                            PingReply pingrep = ping.Send(parts[0].Trim());
                            if (pingrep.Status == IPStatus.Success)
                            {
                                foundone = true;
                            }
                        }
                        catch { }
                        break;
                    }
                }
                s = sr.ReadLine();
            }
        }
    }

    return foundone;
}
有帮助吗?

解决方案

一种不同的方法是使用 pingarp 工具。由于ARP报只能停留在同样广播领域,可以平网络的广播地址以及每一个客户将答复ARP响应。每一个这些反应是缓存在ARP表,你可以查看的命令 arp -a.所以破败:

rem Clear ARP cache
netsh interface ip delete arpcache

rem Ping broadcast addr for network 192.168.1.0
ping -n 1 192.168.1.255

rem View ARP cache to see if the MAC addr is listed
arp -a

这些可以在管理代码,如在 System.Net.NetworkInformation 名字空间。

注:清除ARP缓可能具有边缘影响上的网络性能通过清除缓存项目的其他地方服务器。然而,通常是缓清除,每20分钟或更短。

其他提示

您可以使用 RARP协议来广播的谁拥有该MAC地址的请求。业主将其IP地址进行响应。我不知道一个RARP工具或图书馆推荐的,所以你可以写你自己的网络层发送/接收这些数据包。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top