创建类UdpGetClientMacAddr.java
package com.shuzhiqiang.common;import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;/*** 主机A向主机B发送“UDP-NetBIOS-NS”询问包,即向主机B的137端口,发Query包来询问主机B的NetBIOS Names信息。* 其次,主机B接收到“UDP-NetBIOS-NS”询问包,假设主机B正确安装了NetBIOS服务........... 而且137端口开放,则主机B会向主机A发送一个“UDP-NetBIOS-NS”应答包,即发Answer包给主机A。* 并利用UDP(NetBIOS Name Service)来快速获取远程主机MAC地址的方法*/
public class UdpGetClientMacAddr {private String sRemoteAddr;private int iRemotePort = 137;private byte[] buffer = new byte[1024];private DatagramSocket ds = null;public UdpGetClientMacAddr(String strAddr) throws Exception {sRemoteAddr = strAddr;ds = new DatagramSocket();}public final DatagramPacket send(final byte[] bytes) throws IOException {DatagramPacket dp = new DatagramPacket(bytes, bytes.length, InetAddress.getByName(sRemoteAddr), iRemotePort);ds.send(dp);return dp;}public final DatagramPacket receive() throws Exception {DatagramPacket dp = new DatagramPacket(buffer, buffer.length);ds.receive(dp);return dp;}public byte[] GetQueryCmd() throws Exception {byte[] t_ns = new byte[50];t_ns[0] = 0x00;t_ns[1] = 0x00;t_ns[2] = 0x00;t_ns[3] = 0x10;t_ns[4] = 0x00;t_ns[5] = 0x01;t_ns[6] = 0x00;t_ns[7] = 0x00;t_ns[8] = 0x00;t_ns[9] = 0x00;t_ns[10] = 0x00;t_ns[11] = 0x00;t_ns[12] = 0x20;t_ns[13] = 0x43;t_ns[14] = 0x4B;for (int i = 15; i < 45; i++) {t_ns[i] = 0x41;}t_ns[45] = 0x00;t_ns[46] = 0x00;t_ns[47] = 0x21;t_ns[48] = 0x00;t_ns[49] = 0x01;return t_ns;}public final String GetMacAddr(byte[] brevdata) throws Exception {// 获取计算机名int i = brevdata[56] * 18 + 56;String sAddr = "";StringBuffer sb = new StringBuffer(17);// 先从第56字节位置,读出Number Of Names(NetBIOS名字的个数,其中每个NetBIOS Names Info部分占18个字节)// 然后可计算出“Unit ID”字段的位置=56+Number Of Names×18,最后从该位置起连续读取6个字节,就是目的主机的MAC地址。for (int j = 1; j < 7; j++) {sAddr = Integer.toHexString(0xFF & brevdata[i + j]);if (sAddr.length() < 2) {sb.append(0);}sb.append(sAddr.toUpperCase());if (j < 6) sb.append(':');}return sb.toString();}public final void close() throws Exception {ds.close();}public final String GetRemoteMacAddr() throws Exception {byte[] bqcmd = GetQueryCmd();send(bqcmd);DatagramPacket dp = receive();String smac = GetMacAddr(dp.getData());close();return smac;}public static void main(String args[]) throws Exception {
// UdpGetClientMacAddr umac = new UdpGetClientMacAddr("127.0.0.1");UdpGetClientMacAddr umac = new UdpGetClientMacAddr("192.168.2.32");System.out.println(umac.GetRemoteMacAddr());}
}
用GetMacController.java实现restful接口调用获取MAC地址返回值
package com.shuzhiqiang.controller;import com.shuzhiqiang.common.ReturnCodes;
import com.shuzhiqiang.common.UdpGetClientMacAddr;
import com.shuzhiqiang.util.Response;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;@RequestMapping("/getMac")
@Controller
public class GetMacController {/*获取本地mac地址*/@RequestMapping("/local")@ResponseBodypublic Object local(@RequestParam(name = "ip", defaultValue = "127.0.0.1") String ip) throws Exception {UdpGetClientMacAddr mac = new UdpGetClientMacAddr(ip);String macAddress = mac.GetRemoteMacAddr();Response response = new Response(ReturnCodes.success, macAddress);return response;}//测试----------------------------------------public static void main(String[] args) throws Exception {UdpGetClientMacAddr mac = new UdpGetClientMacAddr("192.168.2.32");//UdpGetClientMacAddr mac = new UdpGetClientMacAddr("192.168.2.25");System.out.println(mac.GetRemoteMacAddr());}
}
菜鸟一枚,正在学习C++ Gui Qt4,整理很零碎,欢迎批评指正 1.窗口标题: QWidget *window = new QWidget; window->setWindowTitle("Enter Your Age"); **************************************** 关于标题...
将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 示例: 输入:1->2->4, 1->3->4 输出:1->1->2->3->4->4 总体思路是: 比较两个链表头节点,较小的插入新链表指针之后,同时较小链表指针向后移动一位 实现如下: ListNode* mergeTwo...
1.直接调用微软socket对象处理 static void Main(string[] args){try{IPAddress ip = new IPAddress(new byte[] { 127, 0, 0, 1 });//在3721端口新建一个TcpListener对象TcpListener listener = new...
现在很多地方都会用到zookeeper, 用到它的地方就是为了实现分布式。用到的场景就是服务注册,比如一个集群服务器,需要知道哪些服务器在线,哪些服务器不在线。 ZK有一个功能,就是创建临时节点,当机器启动应用的时候就会连接到一个ZK节点,然后创建一个临时节点,那么通过获取监听该路径,并且获取该路径下的节点数量就知道有哪些服务...
前台到后台java时data日期类型的转化 在实体类中用@DataTimeFormat,这样设置即使传过来是空的字符串也是可以转的,要和前面传过来的格式一致,如 @XmlElement(name="BeginDate") @DateTimeFormat(pattern="yyyy-MM-dd") private Date begin...