获取wifi用户列表

时间:2015-12-09 14:28:14

标签: c# winforms wifi

我有一个TP-Link wifi路由器(150M) 我通过LAN Wire与朋友分享我的wifi连接...意味着我只是在我的一个路由器(主/主路由器)端口/插槽中插入LAN线,另一端插入我朋友的路由器插槽(辅助)。

它工作正常。

现在,我可以看到有多少用户连接到我的路由器(主要) 但我只能看到我朋友的路由器连接/在线...没有显示有多少用户/设备连接到他的路由器。

我的意思是说我的辅助路由器是否连接到主路由器......它必须显示通过主路由器连接的所有用户......

我做了一些编码以获取路由器的已连接用户列表;它的工作正常。

private void Form1_Load(object sender, EventArgs e)
{
    lstLocal.View = View.Details;
    lstLocal.Clear();
    lstLocal.GridLines = true;
    lstLocal.FullRowSelect = true;
    lstLocal.BackColor = System.Drawing.Color.Aquamarine;
    lstLocal.Columns.Add("IP", 100);
    lstLocal.Columns.Add("HostName", 200);
    lstLocal.Columns.Add("MAC Address", 300);
    lstLocal.Sorting = SortOrder.Descending;
    Ping_all();   //Automate pending
}

static string NetworkGateway()
{
    string ip = null;

    foreach (NetworkInterface f in NetworkInterface.GetAllNetworkInterfaces())
    {
        if (f.OperationalStatus == OperationalStatus.Up)
        {
            foreach (GatewayIPAddressInformation d in f.GetIPProperties().GatewayAddresses)
            {
                if(ip==null)
                ip = d.Address.ToString();
            }
        }
    }
    return ip;
}

public void Ping_all()
{
    string gate_ip = NetworkGateway();
    //Extracting and pinging all other ip's.
    string[] array = gate_ip.Split('.');

    for (int i = 0; i <= 255; i++)
    {
        string ping_var = array[0] + "." + array[1] + "." + array[2] + "." + i;
        //time in milliseconds           
        Ping(ping_var, 1, 4000);
    }
}

public void Ping(string host, int attempts, int timeout)
{
    for (int i = 0; i < attempts; i++)
    {
        new Thread(delegate()
        {
            try
            {
                System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
                ping.PingCompleted += new PingCompletedEventHandler(PingCompleted);
                ping.SendAsync(host, timeout, host);
            }
            catch
            {
                // Do nothing and let it try again until the attempts are exausted.
                // Exceptions are thrown for normal ping failurs like address lookup
                // failed.  For this reason we are supressing errors.
            }
        }).Start();
    }
}

private void PingCompleted(object sender, PingCompletedEventArgs e)
{
    string ip = (string)e.UserState;
    if (e.Reply != null && e.Reply.Status == IPStatus.Success)
    {
        string hostname = GetHostName(ip);
        string macaddres = GetMacAddress(ip);
        string[] arr = new string[3];

        //store all three parameters to be shown on ListView
        arr[0] = ip;
        arr[1] = hostname;
        arr[2] = macaddres;

        // Logic for Ping Reply Success
        ListViewItem item;
        if (this.InvokeRequired)
        {
            this.Invoke(new Action(() =>
            {
                item = new ListViewItem(arr);
                if (lstLocal.Items.Contains(item))
                {
                }
                else
                {
                    lstLocal.Items.Add(item);
                }
            }));
        }
    }
    else
    {
        // MessageBox.Show(e.Reply.Status.ToString());
    }
}

public string GetHostName(string ipAddress)
{
    try
    {
        IPHostEntry entry = Dns.GetHostEntry(ipAddress);
        if (entry != null)
        {
            return entry.HostName;
        }
    }
    catch (SocketException)
    {
        // MessageBox.Show(e.Message.ToString());
    }
    return null;
}

//Get MAC address
public string GetMacAddress(string ipAddress)
{
    string macAddress = string.Empty;
    System.Diagnostics.Process Process = new System.Diagnostics.Process();
    Process.StartInfo.FileName = "arp";
    Process.StartInfo.Arguments = "-a " + ipAddress;
    Process.StartInfo.UseShellExecute = false;
    Process.StartInfo.RedirectStandardOutput = true;
    Process.StartInfo.CreateNoWindow = true;
    Process.Start();
    string strOutput = Process.StandardOutput.ReadToEnd();
    string[] substrings = strOutput.Split('-');
    if (substrings.Length >= 8)
    {
        macAddress = substrings[3].Substring(Math.Max(0, substrings[3].Length - 2))
                 + "-" + substrings[4] + "-" + substrings[5] + "-" + substrings[6]
                 + "-" + substrings[7] + "-"
                 + substrings[8].Substring(0, 2);
        return macAddress;
    }
    else
    {
        return "My Machine";
    }
}

任何人都可以告诉我如何获得辅助路由器用户列表。

0 个答案:

没有答案