当前位置:首页 > Windows程序 > 正文

C#获取局域网IP、MAC地址和端口的初学

2021-03-25 Windows程序

首先非常感谢Melou的的随笔,对于初学C#的我,参考你的随笔对我的学习真是有莫大帮助。

C#遍历局域网的几种方法:

1、微软社区上介绍了使用Active Directory 来遍历局域网

首先我们来了解DirectoryEntry类是一个什么类。

命名空间:  System.DirectoryServices
程序集:  System.DirectoryServices(在 System.DirectoryServices.dll 中)

DirectoryEntry类可封装Active Directory 域服务层次结构中的节点或对象。

其中有构造函数

DirectoryEntry(String),初始化 DirectoryEntry 类的新实例,该类将此实例绑定到位于指定路径的 Active Directory 域服务中的节点。

链接:https://msdn.microsoft.com/zh-cn/library/system.directoryservices.directoryentry.aspx

我们必须了解其中的Active Directory

使用 Active Directory(R) 域服务 (AD DS) 服务器角色,可以创建用于用户和资源管理的可伸缩、安全及可管理的基础机构,并可以提供对启用目录的应用程序(如 Microsoft(R) Exchange Server)的支持。

AD DS 提供了一个分布式数据库,该数据库可以存储和管理有关网络资源的信息,以及启用了目录的应用程序中特定于应用程序的数据。运行 AD DS 的服务器称为域控制器。管理员可以使用 AD DS 将网络元素(如用户、计算机和其他设备)整理到层次内嵌结构。内嵌层次结构包括 Active Directory 林、林中的域以及每个域中的组织单位 (OU)。

链接:https://technet.microsoft.com/zh-cn/library/hh831484.aspx

接下来看怎么利用DirectoryEntry

利用DirectoryEntry组件来查看网络

//C# 操作WINNT本地用户和组 DirectoryEntry DEMain = new DirectoryEntry("WinNT:"); foreach(DirectoryEntry DEGroup in DEMain.Children) { //如果和选择的用户组相同 if(DEGroup.Name.ToLower() == comb_workgroup.Text.ToLower()) { //读出用户组下的每一个主机名 foreach(DirectoryEntry DEComputer in DEGroup.Children) { //滤掉无效结果 Computer: Schema if(DEComputer.Name.ToLower() != "schema") { listb_computer.Items.Add(DEComputer.Name); } } } }

效果评价:速度慢,效率低,还有一个无效结果 Computer: Schema 使用的过程中注意虑掉。

2、利用Dns.GetHostByAddress和IPHostEntry遍历局域网

Dns.GetHostByAddress类:返回指定主机的 Internet 协议 (IP) 地址。

命名空间:  System.Net
程序集:  System(在 System.dll 中)

public static IPAddress[] GetHostAddresses( string hostNameOrAddress )

IPHostEntry类:为 Internet 主机地址信息提供容器类。

命名空间:  System.Net
程序集:  System(在 System.dll 中)

private void EnumComputers2() { //从192.168.0.1 到 192.168.0.255开始依次尝试 for (int i = 1; i <= 255; i++) { string scanIP = "192.168.0." + i.ToString(); IPAddress myScanIP = IPAddress.Parse(scanIP); IPHostEntry myScanHost = null; try { //获取myScanIP的IP地址 myScanHost = Dns.GetHostByAddress(myScanIP); } catch { continue; } if (myScanHost != null) { //返回IP和主机名 Console.WriteLine(scanIP + "|" + myScanHost.HostName); } } }

效果评价:效率低,速度慢,不是一般的慢。

3、使用System.Net.NetworkInformation.Ping来遍历局域网

System.Net.NetworkInformation.Ping类:允许应用程序确定是否可通过网络访问远程计算机。

命名空间:System.Net.NetworkInformation
程序集:System(在 system.dll 中)

delegate void WT(int n); Thread t; private string GetMacAddress(string hostip)//获取远程IP(不能跨网段)的MAC地址 { string Mac = ""; try { Int32 ldest = inet_addr(hostip); //将IP地址从 点数格式转换成无符号长整型 Int64 macinfo = new Int64(); Int32 len = 6; //SendARP函数发送一个地址解析协议(ARP)请求获得指定的目的地IPv4地址相对应的物理地址 SendARP(ldest, 0, ref macinfo, ref len); string TmpMac = Convert.ToString(macinfo, 16).PadLeft(12, ‘0‘);//转换成16进制  注意有些没有十二位 Mac = TmpMac.Substring(0, 2).ToUpper();// for (int i = 2; i < TmpMac.Length; i = i + 2) { Mac = TmpMac.Substring(i, 2).ToUpper() + "-" + Mac; } } catch (Exception Mye) { Mac = "获取远程主机的MAC错误:" + Mye.Message; } return Mac; } private void EnumComputers(int n) { //lock (this) //Invoke(new MethodInvoker(delegate() //{ //注册委托 if (this.txtAddrs.InvokeRequired) { WT d = new WT(EnumComputers); this.Invoke(d, new object[] { n }); } else { try { for (int i = 0; i <= 255; i++) { Ping myPing; myPing = new Ping(); //当发送 Internet 控制消息协议 (ICMP) 回送消息并接收相应 ICMP 回送答复消息的异步操作完成或被取消时发生。 myPing.PingCompleted += new PingCompletedEventHandler(myPing_PingCompleted); string pingIP = "192.168." + n.ToString() + "." + i.ToString(); //尝试以异步方式向指定的计算机发送Internet控制消息协议(ICMP)回送消息,,并从该计算机接收相应的ICMP回送答复消息。 myPing.SendAsync(pingIP, 1000, null); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } //})); } private void myPing_PingCompleted(object sender, PingCompletedEventArgs e) { StringBuilder sb = new StringBuilder(); if (e.Reply.Status == IPStatus.Success) { sb.Append("IP:" + e.Reply.Address.ToString() + "\r\n"); //获取MAC地址 string mac = GetMacAddress(e.Reply.Address.ToString()); sb.Append("MAC:" + mac + "\r\n\r\n"); num++; } this.txtAddrs.Text += sb.ToString(); //this.lbIPs.Text = "在线IP数:" + num.ToString(); this.lbIPs.Text = num.ToString(); } private void button2_Click(object sender, EventArgs e) { this.txtAddrs.Text = ""; num = 0; t = new Thread(() => EnumComputers((int)(this.txtIP2.Value))); t.IsBackground = true; t.Start(); //EnumComputers((int)(this.txtIP2.Value)); }

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/67269.html