ARP扫描局域网主机ip和MAC(c#) 下载本文

内容发布更新时间 : 2024/5/17 17:23:42星期一 下面是文章的全部内容请认真阅读。

LANScanner Project Report Project No. X

Version 1.0

Student ID: 93520081202015 Student Name:银国徽

2010-10-26

College of Computing, CUC

I declare that the assignment here submitted is original except for source material explicitly acknowledged. I also acknowledge that I am aware of University policy and regulations on honesty in academic work, and of the disciplinary guidelines and procedures applicable to breaches of such policy and regulations. Name: 银国徽 Student ID:103520430112010

Section I Problem Specification

本次实验的任务是搜索局域网的所有主机,获取其MAC,并能时刻获取主机是否开机 的信息。要求采用可视化界面进行设计。

Section II Solution Method and Design

第一步:获取本机IP和本机所在局域网网段的子网掩码sub-mask;

第二步:写一个算法,根据上一步的信息获取整个网段所有的IP,尽量避免进行简单

的与运算,因为当子网掩码为非255.255.255.0时,计算量非常大,为了使程序获得更快的 速度和稳定性,需要一个小技巧。原理很简单:一个网段的IP总是连续的。利用这个原理 可以大大简化计算。举例如下:

本机的IP是10.128.9.230,子网掩码为255.255.252.0;

那么本网段的可用IP范围是10.128.8.0---10.128.11.255。这里面的规律是很有趣的,大 家可以算算看。

第三步:利用上步所得的结果构造ARP包,获取相应的MAC; 第四步:利用多线程技术,快速实现ARP的运行;

第五步:将返回的结果利用委托技术显示到主窗口上。

Section III Test Cases and Results Analysis

Section IV Conclusion

通过本次实验,我对C#、多线程、委托、整个程序的运行次序有了更深入的理解,感 谢老师给予的指导,我会通过学习C#了解C++的运行方式,因为他们的模式太相似了。

Section V References

C#多线程计算机扫描 电脑爱好者 C#.net实训编程 百度文库

C# 如何理解多线程和委托 百度文库 C# 数据类型转换 百度文库

C++和C#互调用DLL 豆丁文档 C# raw socket 网络封包监视 CSDN

Section VI Appendix

using System;

using System.Management;

using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;

using System.Windows.Forms;

using System.Threading;//使用多线程的声明(准确的说是要在main主线程中建立新的线程时要进行的引 用说明)

using System.Runtime.InteropServices;//使用DllImport的空间引用声明 using System.Net;//对IP相关类的引用声明 namespace mytest {

public partial class Form1 : Form {

[DllImport(\

public static extern uint SendARP(uint DestIP, uint SrcIP, ref ulong pMacAddr, ref uint

PhyAddrLen);

public static int Num = 0; byte [] submask={0,0,0,0}; byte[] localip = { 0,0,0,0}; byte[] netip = { 0,0,0,0}; int [] temp={0,0,0,0};

byte[] destip = { 0,0,0,0}; public static string IP; public static string Mac;

public static string ItemFlag;

//Thread thread1; //在主线程中声明线程1 // Thread thread2; //在主线程中声明线程2 delegate void daililist();//主线程之外的线程要调用main线程中建立的控件,需要使用委托

的方式,此处是委托的定义

public delegate void UpdateList(string ip,string result); public delegate void UpdateLabel();

//daililist list1;//委托实例化,即建立委托对应的事件