User Tools

Site Tools


info:wake_on_lan_java

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
info:wake_on_lan_java [2008/07/05 19:28] tomgeeinfo:wake_on_lan_java [2008/07/13 19:50] (current) tomgee
Line 5: Line 5:
  
 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. 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+ 
 +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. 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+ 
 +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. 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.
Line 16: Line 18:
     * Each subsequent set of 6 bytes is also filled with the MAC address of the target computer, until the packet is full.     * 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+ 
 +===== 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. 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. 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+ 
 +Finding out your MAC address\\
  
 If you are running Windows, your MAC address will be revealed when you type ipconfig /all into a command prompt. You'll be looking for a line similar to this: If you are running Windows, your MAC address will be revealed when you type ipconfig /all into a command prompt. You'll be looking for a line similar to this:
Line 27: Line 32:
     Physical Address. . . . . . . . . : 00-0E-62-09-23-4B     Physical Address. . . . . . . . . : 00-0E-62-09-23-4B
  
-Writing the Wake-on-LAN Java program+===== Writing the Wake-on-LAN Java program ===== 
  
 Let's write a simple program to do Wake-on-LAN. Let's write a simple program to do Wake-on-LAN.
Line 39: Line 45:
  
 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. 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 
  
 Download WakeOnLan.java Download WakeOnLan.java
Line 62: Line 67:
  
 You should now see the target machine booting up as if by magic.  You should now see the target machine booting up as if by magic. 
 +
 +----
 +
 +
 +
 +
 +
 +
 +
 +===== Source Code =====
 +
 +    import java.io.*;
 +    import java.net.*;
 +
 +    public class WakeOnLan {   
 +    public static final int PORT = 9;    
 +    
 +    public static void main(String[] args) {
 +        
 +        if (args.length != 2) {
 +            System.out.println("Usage: java WakeOnLan <broadcast-ip> <mac-address>");
 +            System.out.println("Example: java WakeOnLan 192.168.0.255 00:0D:61:08:22:4A");
 +            System.out.println("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 {
 +            byte[] macBytes = getMacBytes(macStr);
 +            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);
 +            }
 +            
 +            InetAddress address = InetAddress.getByName(ipStr);
 +            DatagramPacket packet = new DatagramPacket(bytes, bytes.length, address, PORT);
 +            DatagramSocket socket = new DatagramSocket();
 +            socket.send(packet);
 +            socket.close();
 +            
 +            System.out.println("Wake-on-LAN packet sent.");
 +        }
 +        catch (Exception e) {
 +            System.out.println("Failed to send Wake-on-LAN packet: + e");
 +            System.exit(1);
 +        }
 +        
 +    }
 +    
 +    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;
 +    }
 +    
 +   
 +}
 +
 +
info/wake_on_lan_java.1215300506.txt.gz · Last modified: 2008/07/05 19:28 by tomgee