zoukankan      html  css  js  c++  java
  • 根据ip地址获得Mac地址的一种方法

    Code
    public static class MacUtility
    {
        [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);

        
    public static string GetRemoteMac(string clientIP)
        {
            
    string ip = clientIP;

            
    if (ip == "127.0.0.1")

                ip 
    = GetLocalIP()[0];

            var ldest 
    = inet_addr(ip);

            Int64 macinfo 
    = new Int64();
            Int32 len 
    = 6;
            
    try
            {
                SendARP(ldest, 
    0ref macinfo, ref len);
            }
            
    catch
            {
                
    return "";

            }
            
    string originalMACAddress = Convert.ToString(macinfo, 16);
            
    if (originalMACAddress.Length < 12)
            {

                originalMACAddress 
    = originalMACAddress.PadLeft(12'0');

            }

            
    string macAddress;

            
    if (originalMACAddress != "0000" && originalMACAddress.Length == 12)
            {
                
    string mac1, mac2, mac3, mac4, mac5, mac6;
                mac1 
    = originalMACAddress.Substring(102);
                mac2 
    = originalMACAddress.Substring(82);
                mac3 
    = originalMACAddress.Substring(62);
                mac4 
    = originalMACAddress.Substring(42);
                mac5 
    = originalMACAddress.Substring(22);
                mac6 
    = originalMACAddress.Substring(02);
                macAddress 
    = mac1 + "-" + mac2 + "-" + mac3 + "-" + mac4 + "-" + mac5 + "-" + mac6;
            }
            
    else
            {
                macAddress 
    = "";
            }
            
    return macAddress.ToUpper();

        }
        
    public static string[] GetLocalIP()
        {
            
    string hostName = Dns.GetHostName();

            IPHostEntry ipEntry 
    = Dns.GetHostByName(hostName);

            IPAddress[] arr 
    = ipEntry.AddressList;

            
    string[] result = new string[arr.Length];

            
    for (int i = 0; i < arr.Length; i++)
            {

                result[i] 
    = arr[i].ToString();
            }
            
    return result;
        }


    }
  • 相关阅读:
    常用快捷键知识汇总
    按照给定区间产生指定数目的随机数—C#
    OC (3) 类 与 类方法 (便利构造器) iOS
    OC (2) 基础知识分析对象,创建类,自定义初始化方法 iOS
    OC (1) 类和对象:ObjectiveC概述、面向对象编程、类和对象、实例变量操作 iOS
    OC (6) Block、数组高级:Block语法、Block使用、Block实现数组排序 iOS
    OC (7) 类的扩展 iOS
    OC (5) 字典、集、数组排序:字典类、集合类、数组数组排序、字典、集合的快速遍历、数组排序 iOS
    OC (4)NSString、NSArray、NSNumber、使用苹果帮助文档、值对象 iOS
    jquery 里 $(this)的用法
  • 原文地址:https://www.cnblogs.com/nuaalfm/p/1553520.html
Copyright © 2011-2022 走看看