0

I am trying to implement WOL using java and below is my code.

public void wakeonlan(String ipaddress, String macaddress) {

        try {
            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);
            }

            InetAddress address = InetAddress.getByName(ipaddress);
            DatagramPacket packet = new DatagramPacket(bytes, bytes.length, address, PORT);
            DatagramSocket socket = new DatagramSocket();
            socket.send(packet);
            socket.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

My requirement is to run a script once the PC is started.Is it possible to get the below details.

  1. A response from the machine after it accepts the magic packet.

  2. How long does a PC takes to startup and how long I need to wait.

  3. Instead of sending it over the network to all machines, can I send this to the specific machine and get the response from it.

Shamnad P S
  • 1,095
  • 2
  • 15
  • 43
  • 2
    WOL is built into your mainboard and/or network adapter, it is not possible to run anything there, including a java app, of course. – specializt Jul 24 '17 at 10:45
  • A C#/C++ Service might be able to notice that the PC finished starting.(it has to be on the target system) Java should only be able to send the WOL and then ping for the pc until it responds but that is not very trustworthy... – Nico Jul 24 '17 at 10:59

1 Answers1

0

WakeOnLAN is implemented using UDP and it is impossible to get a response from it by design.

The only thing you can do is to create a loop that waits for host to get back on (boot or resume).

There is no guarantee that the host will ever wake up because it may have never received the message, or it may failed to boot and tons of other possible causes.

sorin
  • 161,544
  • 178
  • 535
  • 806