Simple Java Implementation of Wake-on-LAN
Wake-on-LAN is fantastic. It lets you turn on a computer via the network, which is great when you need to turn on your home machine while you're at work. Most PCs these days support Wake-on-LAN.
If your network socket still shows a green light after you've shut down computer, it probably supports Wake-on-LAN. The motherboard uses a small amount of power to monitor network traffic and look for special Wake-on-LAN packets. If it sees one, it will power up the system as if you had just pressed the on switch.
Creating a Wake-on-LAN packet
Some people are surprised how difficult it is to find out how to create a Wake-on-LAN packet. There are a few oddities about it, sure, but this guide will hopefully explain what you need to do. I'll show you how to create a simple Java program that sends a Wake-on-LAN packet to wake up a specified machine. This program can easily be translated into other languages, but Java offers the platform independence which is useful in a networked environment.
UDP and MAC addresses
A Wake-on-LAN packet is an ordinary UDP packet which contains the MAC address of the target computer. For reasons unknown to me, the UDP packet must be 16 times larger than the byte representation of the MAC address, plus an extra 6 bytes for a header. A MAC address is usually specified as a string of hexadecimal digits, for example 00:0D:61:08:22:4A, so can be represented using just 6 bytes. This makes the total packet size 6 + 16*6 = 102 bytes.
- The first 6 bytes of the packet are filled with 0xff. I'm not sure why!
- The next 6 bytes are the MAC address of the target computer
- Each subsequent set of 6 bytes is also filled with the MAC address of the target computer, until the packet is full.
Sending the Magic Packet
The UDP packet is sent to a broadcast address, such as 192.168.0.255. This will cause it to be received by all computers on your local LAN, but only those with a matching MAC address will respond by powering on. MAC addresses are associated with each network interface and are typically unique. The UDP packet uses port 9.
Note that the delivery of a UDP packet is not guaranteed. You may need to send more than one Wake-on-LAN packet if you are using a busy network.
Finding out your MAC address
If you are running Windows, your MAC address will be revealed when you
ipconfig /all into a command prompt. You'll be looking
for a line similar to this:
Physical Address. . . . . . . . . : 00-0E-62-09-23-4B
Writing the Wake-on-LAN Java program
Let's write a simple program to do Wake-on-LAN.
All it needs to do is accept two piece of user input: an IP address and a MAC address. The Wake-on-LAN packet will be broadcast to the IP address and will only wake up the computer with a matching MAC address.
To make it more user friendly, the program will accept either of the two typical ways of expressing a MAC address, for example:
All you need to do is provide a handy method to turn strings like these into 6-byte representations. This is a simple case of converting hex into decimal. The MAC address can then be bundled off into a UDP packet and sent to the target machine.
Download the Wake-on-LAN code
Save the WakeOnLan.java file to your hard disk.
Compile the program with:
% javac *.java
Now run the program without any parameters and it will now remind you about the two parameters you need to supply on the command line:
% java WakeOnLan Usage: java WakeOnLan
Example: java WakeOnLan 192.168.0.255 00:0E:62:09:23:4B Example: java WakeOnLan 192.168.0.255 00-0E-62-09-23-4B
Make sure you know the MAC address of the target machine, and give it to the program. If all went well, it should send the magic packet successfully:
% java WakeOnLan 192.168.0.255 00-0E-62-09-23-4B Wake-on-LAN packet sent.
You should now see the target machine booting up as if by magic.
Search this site
Copyright Paul Mutton 2001-2010