|
Sep 24
|
Wake-on-LAN is an Ethernet computer networking standard that allows a computer to be turned on or woken up by a network message. The message is usually sent by a simple program executed on another computer on the local area network.
BIOS Setup
You first need to enable WOL in your bios. This might be a Wake on LAN setting under power management, or it might be something like “Allow power from S5 by PME”. S5 is a power management state and PME is Power Management Event.
Computer Setup
You need to make sure you allow your network device to wake up your pc. In windows you can do this through the device manager for your network adapter. If you edit the properties for your network card (Control Panel > Device Manager > Network Adapters > Your Ethernet card > Properties > Power Management) – you can check a box to ‘Allow this device to wake up the computer’ and you can force it only to allow WOL magic packets to wake up the pc.
Routers
If your pc is behind a router, then you will need to forward all requests to port 9 to your pc that you want to wake up. Some routers allow you to assign applications to certain devices, and you may see ‘Wake On LAN’ as one of the applications.
Java code to wake up your pc
Below is some java code that you can use to wake up your pc using wake on lan. You will need to know your IP address and the MAC address of your network card.
package com.devedup.net;
import org.apache.log4j.Logger;
import java.io.*;
import java.net.*;
/**
* Used for waking up computers
*
* @author dave
* @since 9 Apr 2009 08:00:04
*/
public class WakeOnLAN {
private final static Logger logger = Logger.getLogger(WakeOnLAN.class);
public static final int PORT = 9;
public static void main(String[] args) {
if (args.length != 2) {
logger.info("Usage: java WakeOnLan <broadcast-ip> <mac-address>");
logger.info(
"Example: java WakeOnLan 192.168.0.255 00:0D:61:08:22:4A");
logger.info(
"Example: java WakeOnLan 192.168.0.255 00-0D-61-08-22-4A");
System.exit(1);
}
String ipStr = args[0];
String macStr = args[1];
try {
wakeup(ipStr, macStr);
} catch (Exception e) {
logger.info("Failed to send Wake-on-LAN packet: + e");
System.exit(1);
}
}
/**
* Send a Wake On LAN magic packet to ip and mac address specifed
*
* @param ip
* @param macAddress
*/
public static void wakeup(String ip, String macAddress) {
if (ip == null || ip.length() < 1)
throw new IllegalArgumentException(
"ip address cannot be null or blank");
if (macAddress == null || macAddress.length() < 1)
throw new IllegalArgumentException(
"macAddress cannot be null or blank");
byte[] macBytes = getMacBytes(macAddress);
byte[] bytes = new byte[6 + 16 * macBytes.length];
for (int i = 0; i < 6; i++) {
bytes[i] = (byte) 0xff;
}
for (int i = 6; i < bytes.length; i += macBytes.length) {
System.arraycopy(macBytes, 0, bytes, i, macBytes.length);
}
DatagramSocket socket = null;
try {
InetAddress address = InetAddress.getByName(ip);
DatagramPacket packet =
new DatagramPacket(bytes, bytes.length, address, PORT);
if (logger.isInfoEnabled()) {
logger.info("Sending datagram (UDP) packet to ip["
+ address + "] on port[" + PORT + "]");
logger.info("Message bytes [" + bytes + "]");
}
socket = new DatagramSocket();
socket.send(packet);
logger.info("Wake-on-LAN packet sent.");
return;
} catch (UnknownHostException e) {
logger.error(e);
} catch (SocketException e) {
logger.error(e);
} catch (IOException e) {
logger.error(e);
} finally {
if (socket != null && !socket.isClosed()) {
socket.close();
}
}
}
private static byte[] getMacBytes(String macStr)
throws IllegalArgumentException {
byte[] bytes = new byte[6];
String[] hex = macStr.split("(\\:|\\-)");
if (hex.length != 6) {
throw new IllegalArgumentException("Invalid MAC address.");
}
try {
for (int i = 0; i < 6; i++) {
bytes[i] = (byte) Integer.parseInt(hex[i], 16);
}
} catch (NumberFormatException e) {
throw new IllegalArgumentException(
"Invalid hex digit in MAC address.");
}
return bytes;
}
}
