c***@yahoo.co.uk
2008-01-03 21:13:13 UTC
Hi all,
I am currently developing a java application that allows the user to
be able to turn on a computer remotely using Wake On Lan. I have got
the code working, however it is slightly tempromental. The MAC Address
and the IP Address is hard coded into the program at present for
testing purposes. I can quite happily shut the computer down and then
wake it back up again with my program once or twice on the trot but
then it stops working all together. Then will eventually start working
again without me changing any of the code.
I have also a script that wake a computer using Wake On Lan which
works every time without fail so I know that it is not a network
problem.
Below is the code that I am using in order to perform Wake On Lan.
package remoteshutdown;
import java.io.*;
import java.net.*;
public class WakeOnLan {
public void WOL() {
final int PORT = 9;
String ipStr = "10.11.12.123";
String macStr = "00:07:E9:93:18:EB";
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;
}
}
Any help in this matter would be highly appreciated.
Thank you
I am currently developing a java application that allows the user to
be able to turn on a computer remotely using Wake On Lan. I have got
the code working, however it is slightly tempromental. The MAC Address
and the IP Address is hard coded into the program at present for
testing purposes. I can quite happily shut the computer down and then
wake it back up again with my program once or twice on the trot but
then it stops working all together. Then will eventually start working
again without me changing any of the code.
I have also a script that wake a computer using Wake On Lan which
works every time without fail so I know that it is not a network
problem.
Below is the code that I am using in order to perform Wake On Lan.
package remoteshutdown;
import java.io.*;
import java.net.*;
public class WakeOnLan {
public void WOL() {
final int PORT = 9;
String ipStr = "10.11.12.123";
String macStr = "00:07:E9:93:18:EB";
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;
}
}
Any help in this matter would be highly appreciated.
Thank you