zoukankan      html  css  js  c++  java
  • 获取客户端Mac

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Runtime.InteropServices;

    namespace WebApplication1
    {
        public class test
        {
            [DllImport("Iphlpapi.dll")]
            private static extern int SendARP(Int32 dest, Int32 host, ref   Int64 mac, ref   Int32 length);
            [DllImport("Ws2_32.dll")]
            private static extern Int32 inet_addr(string ip);

            ///<summary>
            ///得到客户端ip
            ///</summary>
            ///<returns>ip</returns>
            private string GetClientIP()
            {
                string result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                if (null == result || result == String.Empty)
                {
                    result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                }
                if (null == result || result == String.Empty)
                {
                    result = HttpContext.Current.Request.UserHostAddress;
                }
                return result;
            }


            ///<summary>
            /// 利用客户端ip得到客户端mac
            ///</summary>
            ///<param name="remoteip">客户端ip</param>
            ///<returns>int16类型的mac</returns>
            static private Int64 getremotemac(string remoteip)
            {
                Int32 ldest = inet_addr(remoteip);
                try
                {
                    Int64 macinfo = new Int64();
                    Int32 len = 6;
                    int res = SendARP(ldest, 0, ref   macinfo, ref   len);
                    return macinfo;
                }
                catch (Exception err)
                {
                    Console.WriteLine("error:{0}", err.Message);
                }
                return 0;
            }


            ///<summary>
            /// int64类型的mac转换成正确的客户端mac
            ///</summary>
            ///<returns>mac</returns>
            public string GetClientMAC()
            {
                Int64 macid = getremotemac(GetClientIP());
                if (macid == 0)
                    return "0";
                //string beforeMacAddr = Convert.ToString(macid, 16);
                string beforeMacAddr = macid.ToString("X");
                while (beforeMacAddr.Length < 12)
                {
                    beforeMacAddr = beforeMacAddr.Insert(0, "0");
                }
               // string beforeMacAddr = macid.ToString("X");
                string endMacAddr = "";
                string[] macArray = new string[6];
                for (int i = 0; i < 6; i++)
                {
                        macArray[i] = beforeMacAddr.Substring(i * 2, 2);               
                }
                for (int i = 0; i < 6; i++)
                {
                    endMacAddr += macArray[5 - i] + "-";
                }
                endMacAddr = endMacAddr.Substring(0, endMacAddr.Length - 1);
                endMacAddr = endMacAddr.ToUpper();
                return endMacAddr;
            }
        }
    }

  • 相关阅读:
    软件测试第三次作业
    第一次实验 Junit简单test三角形的小程序
    软件测试[2]falut error failure 的区别与理解
    java中使用jxl的jar包处理excel的复制,更新等问题。
    java中== 和 .equals()的区别
    c# 规格说明书
    c#第九课 linq stream(2)
    c# 第八课 linq stream
    c# 第七课 NET 框架的正则表达式类
    矩阵模版(新)
  • 原文地址:https://www.cnblogs.com/T--Y/p/3146304.html
Copyright © 2011-2022 走看看