取得同一网段内的IP和MAC地址!

 1 [DllImport("Iphlpapi.dll")]
 2         private static extern int SendARP(Int32 dest, Int32 host, ref Int64 mac, ref Int32 length);
 3         [DllImport("Ws2_32.dll")]
 4         private static extern Int32 inet_addr(string ip);
 5 
 6         protected void Page_Load(object sender, EventArgs e)
 7         {
 8             // 在此处放置用户代码以初始化页面
 9             try
10             {
11                 string userip = Request.UserHostAddress;
12                 string strClientIP = Request.UserHostAddress.ToString().Trim();
13                 Int32 ldest = inet_addr(strClientIP); //目的地的ip 
14                 Int32 lhost = inet_addr("");   //本地服务器的ip 
15                 Int64 macinfo = new Int64();
16                 Int32 len = 6;
17                 int res = SendARP(ldest, 0ref macinfo, ref len);
18                 string mac_src = macinfo.ToString("X");
19                 if (mac_src == "0")
20                 {
21                     if (userip == "127.0.0.1")
22                         Response.Write("正在访问Localhost!");
23                     else
24                         Response.Write("欢迎来自IP为" + userip + "的朋友!" + "<br>");
25                     return;
26                 }
27 
28                 while (mac_src.Length < 12)
29                 {
30                     mac_src = mac_src.Insert(0"0");
31                 }
32 
33                 string mac_dest = "";
34 
35                 for (int i = 0; i < 11; i++)
36                 {
37                     if (0 == (i % 2))
38                     {
39                         if (i == 10)
40                         {
41                             mac_dest = mac_dest.Insert(0, mac_src.Substring(i, 2));
42                         }
43                         else
44                         {
45                             mac_dest = "-" + mac_dest.Insert(0, mac_src.Substring(i, 2));
46                         }
47                     }
48                 }
49 
50                 Response.Write("欢迎来自IP为" + userip + "<br>" + ",MAC地址为" + mac_dest + "的朋友!"
51 
52                  + "<br>");
53             }
54             catch (Exception err)
55             {
56                 Response.Write(err.Message);
57             }
posted @ 2011-08-11 10:14  伽马科技.攻城师  阅读(613)  评论(0编辑  收藏  举报
天空网站统计分析系统