Wake On Lan using Unicast
<p>I am trying to implement Wake On Lan using unicast. I have the code for broadcast and it is working fine. But I want to make it work using Unicast and I tried a lot but couldn't achieve. </p>

<p>Here is the broadcast code.</p>

<pre><code>import java.io.*;
import java.net.*;

public class WakeOnLan {

public static final int PORT = 9;    

public static void main(String[] args) {

    String ipStr = "192.168.1.255";
    String macStr = "00-1a-6b-51-0a-8c";

    try {
        byte[] macBytes = getMacBytes(macStr);
        byte[] bytes = new byte[6 + 16 * macBytes.length];
        for (int i = 0; i &lt; 6; i++) {
            bytes[i] = (byte) 0xff;
        }
        for (int i = 6; i &lt; 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 &lt; 6; i++) {
            bytes[i] = (byte) Integer.parseInt(hex[i], 16);
        }
    }
    catch (NumberFormatException e) {
        throw new IllegalArgumentException("Invalid hex digit in MAC address.");
    }
    return bytes;
}


}
</code></pre>

<p>If the machine is off, It will not have an Ip address. Then how can unicast work?</p>
