java获取客户端网卡的MAC地址

转载自:
  1. import java.io.IOException;  
  2. import java.net.DatagramPacket;  
  3. import java.net.DatagramSocket;  
  4. import java.net.InetAddress;  
  5.   
  6. public class UdpGetClientMacAddr {  
  7.     private String sRemoteAddr;  
  8.     private int iRemotePort = 137;  
  9.     private byte[] buffer = new byte[1024];  
  10.     private DatagramSocket ds = null;  
  11.   
  12.     public static void main(String[] args) {  
  13.         try {  
  14.             UdpGetClientMacAddr mac = new UdpGetClientMacAddr("10.0.150.207");  
  15.             String ma = mac.GetRemoteMacAddr();  
  16.             System.out.println(ma);  
  17.         } catch (Exception e) {  
  18.             e.printStackTrace();  
  19.         }  
  20.     }  
  21.   
  22.     public UdpGetClientMacAddr(String strAddr) throws Exception {  
  23.         sRemoteAddr = strAddr;  
  24.         ds = new DatagramSocket();  
  25.     }  
  26.   
  27.     protected final DatagramPacket send(final byte[] bytes) throws IOException {  
  28.         DatagramPacket dp = new DatagramPacket(bytes, bytes.length, InetAddress  
  29.                 .getByName(sRemoteAddr), iRemotePort);  
  30.         ds.send(dp);  
  31.         return dp;  
  32.     }  
  33.   
  34.     protected final DatagramPacket receive() throws Exception {  
  35.         DatagramPacket dp = new DatagramPacket(buffer, buffer.length);  
  36.         ds.receive(dp);  
  37.         return dp;  
  38.     }  
  39.   
  40.     protected byte[] GetQueryCmd() throws Exception {  
  41.         byte[] t_ns = new byte[50];  
  42.         t_ns[0] = 0x00;  
  43.         t_ns[1] = 0x00;  
  44.         t_ns[2] = 0x00;  
  45.         t_ns[3] = 0x10;  
  46.         t_ns[4] = 0x00;  
  47.         t_ns[5] = 0x01;  
  48.         t_ns[6] = 0x00;  
  49.         t_ns[7] = 0x00;  
  50.         t_ns[8] = 0x00;  
  51.         t_ns[9] = 0x00;  
  52.         t_ns[10] = 0x00;  
  53.         t_ns[11] = 0x00;  
  54.         t_ns[12] = 0x20;  
  55.         t_ns[13] = 0x43;  
  56.         t_ns[14] = 0x4B;  
  57.   
  58.         for (int i = 15; i < 45; i++) {  
  59.             t_ns[i] = 0x41;  
  60.         }  
  61.   
  62.         t_ns[45] = 0x00;  
  63.         t_ns[46] = 0x00;  
  64.         t_ns[47] = 0x21;  
  65.         t_ns[48] = 0x00;  
  66.         t_ns[49] = 0x01;  
  67.         return t_ns;  
  68.     }  
  69.   
  70.     protected final String GetMacAddr(byte[] brevdata) throws Exception {  
  71.   
  72.         int i = brevdata[56] * 18 + 56;  
  73.         String sAddr = "";  
  74.         StringBuffer sb = new StringBuffer(17);  
  75.   
  76.         for (int j = 1; j < 7; j++) {  
  77.             sAddr = Integer.toHexString(0xFF & brevdata[i + j]);  
  78.             if (sAddr.length() < 2) {  
  79.                 sb.append(0);  
  80.             }  
  81.             sb.append(sAddr.toUpperCase());  
  82.             if (j < 6)  
  83.                 sb.append(':');  
  84.         }  
  85.         return sb.toString();  
  86.     }  
  87.   
  88.     public final void close() {  
  89.         try {  
  90.             ds.close();  
  91.         } catch (Exception ex) {  
  92.             ex.printStackTrace();  
  93.         }  
  94.     }  
  95.   
  96.     public final String GetRemoteMacAddr() throws Exception {  
  97.         byte[] bqcmd = GetQueryCmd();  
  98.         send(bqcmd);  
  99.         DatagramPacket dp = receive();  
  100.         String smac = GetMacAddr(dp.getData());  
  101.         close();  
  102.   
  103.         return smac;  
  104.     }  
  105. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值