zoukankan      html  css  js  c++  java
  • 取得同一网段内的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             }

  • 相关阅读:
    bash 学习笔记5
    zookeeper启动脚本修改
    java enmu 使用说明
    bash 学习笔记4
    zookeep集群搭建说明
    bash 学习笔记3
    集群配置问题
    hadoop hdfs 总结(1)
    bash 学习笔记6
    bash pitfall
  • 原文地址:https://www.cnblogs.com/chjw8016/p/2134514.html
Copyright © 2011-2022 走看看