java获取客户端网卡的MAC地址_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java获取客户端网卡的MAC地址

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

 2011/8/10 17:20:58  zhyj124  http://zhyj124.iteye.com  我要评论(0)
  • 摘要:转载自:importjava.io.IOException;importjava.net.DatagramPacket;importjava.net.DatagramSocket;importjava.net.InetAddress;publicclassUdpGetClientMacAddr{privateStringsRemoteAddr;privateintiRemotePort=137;privatebyte[]buffer=newbyte[1024]
  • 标签:Java 客户 客户端 Mac

monospace;"> 转载自:

  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. } ?

上一篇: android打电话 下一篇: JAVA 点滴
发表评论
用户名: 匿名