Skip to content

Instantly share code, notes, and snippets.

@rednoah
Last active June 14, 2024 04:46
Show Gist options
  • Select an option

  • Save rednoah/f741760a81bf955b0ef8498ccf3c81c5 to your computer and use it in GitHub Desktop.

Select an option

Save rednoah/f741760a81bf955b0ef8498ccf3c81c5 to your computer and use it in GitHub Desktop.
Wake-on-LAN in Java
public static void wol(String mac) throws IOException {
int[] hex = Pattern.compile("[:-]").splitAsStream(mac).mapToInt(f -> Integer.parseInt(f, 16)).toArray();
byte[] bytes = new byte[6 + 6 * 16];
for (int i = 0; i < 6; i++) {
bytes[i] = (byte) 0xFF;
}
for (int i = 0; i < 16; i++) {
for (int m = 0; m < hex.length; m++) {
bytes[6 + i * hex.length + m] = (byte) hex[m];
}
}
try (DatagramSocket socket = new DatagramSocket()) {
socket.send(new DatagramPacket(bytes, bytes.length, InetAddress.getByName("255.255.255.255"), 9));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment