【问题标题】:How to scan open port of client machine using local IP address in C# in MVC?如何在 MVC 中使用 C# 中的本地 IP 地址扫描客户端计算机的开放端口?
【发布时间】:2016-12-07 11:32:37
【问题描述】:

我正在用 MVC 开发一个 Web 应用程序。当用户登录网站时,我正在获取用户的本地 IP 地址。但现在我要做的是扫描客户端本地机器的开放端口列表。

有没有什么方法可以在 MVC 中的 JavaScript 或 jQuery 或 C# 中实现这一点?

【问题讨论】:

  • 您首先会尝试做什么?您查阅了哪些文档、教程或示例?你在哪里卡住?如果有的话,你的代码的相关部分是什么样的?

标签: javascript c# asp.net-mvc port


【解决方案1】:
 public void btnPing_Click(object sender, EventArgs e)
    {
        try
        {
            int intCount = 0;
            int maxip = 0;
            richTmp.Clear();
            string strIP = null;
            string machineName = string.Empty;
            List<string> strListIp = new List<string>();
            List<string> txtBuffrFile = new List<string>();
            BufferdIp = new List<string>();

            for (int i = 1; i <= 30; i++)
            {
               System.Net.NetworkInformation.Ping p = new System.Net.NetworkInformation.Ping();
               lstDetails.Items.Add("System Found: " + maxip);
               System.Net.NetworkInformation.PingReply rep = p.Send("192.168.1." + i,500);
                if (rep.Status == System.Net.NetworkInformation.IPStatus.Success)
                {
                    intCount++;
                    lstDetails.Items.Clear();
                    lstDetails.Items.Add("Loading...Total(" + maxip+")");
                    strIP = rep.Address.ToString();
                    machineName = GetMachineNameFromIPAddress(strIP);
                   if (machineName == string.Empty || machineName == null)
                    {
                        strListIp.Add(strIP + ":-" + "Offline");
                        BufferdIp.Add("0");
                    }
                    else
                    {
                        strListIp.Add(strIP + ":-" + machineName+"(Online)");
                        BufferdIp.Add(strIP);
                        txtBuffrFile.Add(strIP);
                        maxip++;
                    }
                }

            }
            lstDetails.Items.Clear();
            foreach (var IDAddres in strListIp)
            {
                lstDetails.Items.Add(IDAddres);
            }
            for (int i = 0; i != txtBuffrFile.Count;i++ )
            {
                richTmp.Text += txtBuffrFile.ElementAt(i) + "\n";
            }
            if (MessageBox.Show("Do you want to Save This Search", "Save Dialog", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == (DialogResult.Yes))
            {
                if(File.Exists (path))
                File.Delete(path);
                FileStream stream = File.Create(path);
                stream.Close();
                File.WriteAllText(path, richTmp.Text, Encoding.UTF8);

            }
            btnConnct.Enabled = true;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

我用它在我的 LAN 区域 ping,但问题是这只有在你知道 IP 模式和范围时才有效:(

【讨论】:

    猜你喜欢
    • 2019-03-05
    • 1970-01-01
    • 2017-10-15
    • 1970-01-01
    • 2019-12-04
    • 1970-01-01
    • 2011-01-30
    • 2011-02-04
    • 2011-01-08
    相关资源
    最近更新 更多