如何在MVC中使用C#中的本地IP地址扫描客户端机器的开放端口
本文关键字:客户端 扫描 地址 机器 IP MVC | 更新日期: 2023-09-27 17:59:32
我正在MVC中开发一个web应用程序。当用户登录网站时,我会得到用户的本地IP地址。但现在我想做的是扫描客户端本地机器的打开端口列表。
有什么方法可以在JavaScript、jQuery或MVC中的C#中实现这一点吗?
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());
}
}
我用它在我的局域网区域进行ping,但问题是只有当你知道IP模式和范围时,它才能工作:(