Last active
June 14, 2024 04:46
-
-
Save rednoah/f741760a81bf955b0ef8498ccf3c81c5 to your computer and use it in GitHub Desktop.
Wake-on-LAN in Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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