java wol极其唤醒

package com;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**
 * 远程唤醒
 */
public class Wol {

	// 魔术包的组成
	// 魔术包的组成非常固定,由6对“FF”组成前缀,其余为重复16次的MAC地址组成。例如:试验机的MAC为:“28-D2-44-35-68-A7”,那么魔术包为:
	// “0xFFFFFFFFFFFF28D2443568A728D2443568A728D2443568A728D2443568A728D2443568A728D2443568A728D2443568A728D2443568A728D2443568A728D2443568A728D2443568A728D2443568A728D2443568A728D2443568A728D2443568A728D2443568A7”
	public static void main(String[] args) {
		try {
			String ip = "192.168.50.244";
			String macAddr = "00:11:32:11:22:33";
			int port = 7;
			byte[] macByte = new byte[6];
			String[] ips = macAddr.split("\\:|\\-");
			for (int i = 0; i < 6; i++) {
				macByte[i] = (byte) Integer.parseInt(ips[i], 16);
			}
			// 用来存储网络唤醒数据包
			byte[] bys = new byte[6 + 16 * macByte.length];
			for (int i = 0; i < 6; i++) {
				bys[i] = (byte) 0xff;
			}
			for (int i = 6; i < bys.length; i += macByte.length) {
				System.arraycopy(macByte, 0, bys, i, macByte.length);
			}
			// 将字符形式的IP地址转换成标准的IP地址
			// InetAddress address = InetAddress.getByName(ip);
			InetAddress address = InetAddress.getByName("255.255.255.255");
			// 生成标准的数据报
			DatagramPacket pack = new DatagramPacket(bys, bys.length, address, port);
			// 创建标准套接字,用来发送数据报
			DatagramSocket socket = new DatagramSocket();
			// 发送魔法包
			socket.send(pack);
			socket.close();
			System.out.println("发送结束");
		} catch (Exception e) {
			e.printStackTrace();
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}

}

 

点赞