-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathWakeOnLan.java
More file actions
61 lines (55 loc) · 1.39 KB
/
Copy pathWakeOnLan.java
File metadata and controls
61 lines (55 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package javaforce.utils;
import java.net.*;
import javaforce.*;
/** Service to send out WakeOnLan packets.
*
* @author pquiring
*/
public class WakeOnLan {
private DatagramSocket socket;
/** Initialize WakeOnLan.
*
* Allocates a UDP socket.
*/
public boolean init() {
try {
socket = new DatagramSocket();
socket.setBroadcast(true);
} catch (Exception e) {
JFLog.log(e);
}
return true;
}
/** Wakes a system with provided mac address. */
public void wake(byte[] mac) {
if (mac == null || mac.length != 6) return;
byte[] data = new byte[102];
//FF FF FF FF FF FF
for(int a=0;a<6;a++) {
data[0] = (byte)0xff;
}
//MAC * 16
for(int a=0;a<16;a++) {
System.arraycopy(mac, 0, data, 6 + a*6, 6);
}
try {
DatagramPacket packet = new DatagramPacket(data, 0, data.length);
packet.setAddress(InetAddress.getByName("255.255.255.255"));
packet.setPort(0);
socket.send(packet);
} catch (Exception e) {
JFLog.log(e);
}
}
/** Wakes a system with provided mac address in hex format (0123456789AB). */
public void wake(String mac) {
if (mac == null || mac.length() != 12) return;
byte[] binmac = new byte[6];
for(int a=0;a<6;a++) {
int idx = a * 2;
String p = mac.substring(idx, idx+2);
binmac[a] = Short.valueOf(p, 16).byteValue();
}
wake(binmac);
}
}