http://my.oschina.net/kingfire/blog/156764
Overview
Remote Power On (Wake Onlan) refers to the network implementation of the server or PC to start running, now a lot of network cards are supported by this feature. The simple principle is to send a specially formatted packet to the target host, and then start the system after the target host receives it.
Specific methods.
Setting the BIOS on Wake Onlan (WOL) feature
View the MAC address of the host network card
Connect the host to the Ethernet via the NIC
send a power-on code to boot the system
In fact, through the socket to the target machine to send Magicpacket (Magic Packet), the format of the Magic Packet, contains a continuous 6 bytes of "FF" and repeated 16 times the MAC address. You can use this protocol to make a magicpacket that uses this protocol by filling in "FFFFFFFFFFFF" + 16 consecutive MAC addresses in any protocol packet (such as TCP/IP, IPX packets). The computer wakes up whenever the NIC detects that there is such a fragment anywhere in the packet.
Java code to send a packet
?
12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
public static void main(String[] args)
throws IOException {
int port =
20105
;
String macAddress =
"01-12-43-44-D5-56"
;
// String destIP = "255.255.255.255";// 广播地址
String destIP =
"112.11.15.28"
;
// 广播地址
// 检测 mac 地址,并将其转换为二进制
byte
[] destMac = getMacBytes(macAddress);
if (destMac ==
null
)
return
;
InetAddress destHost = InetAddress.getByName(destIP);
// construct packet data
byte
[] magicBytes =
new byte
[
102
];
// 将数据包的前6位放入0xFF即 "FF"的二进制
for (
int i =
0
; i <
6
; i++)
magicBytes[i] = (
byte
)
0xFF
;
// 从第7个位置开始把mac地址放入16次
for (
int i =
0
; i <
16
; i++) {
for (
int j =
0
; j < destMac.length; j++) {
magicBytes[
6 + destMac.length * i + j] = destMac[j];
}
}
// create packet
DatagramPacket dp =
null
;
dp =
new DatagramPacket(magicBytes, magicBytes.length, destHost, port);
DatagramSocket ds =
new DatagramSocket();
ds.send(dp);
ds.close();
System.out.println(
"ok"
);
}
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;
}
|
Problems that you may encounter
in the router environment, want to be in the public network to achieve internal network computer boot
The router's IP mapping needs to be set to map the extranet address to an intranet address, such as the Tp-link DMZ host setting.
Set the IP address of the destination host (mac:01-12-43-44-d5-56) to a static IP, such as 192.168.0.99, and then bind the Mac and IP on the router
the public IP at home has been changing
You can bind the peanut shell inside the router (the mobile CRC may have to pay for the server to resolve the IP correctly).
can also be used to become, timed access IP address resolution Web pages such as: Http://www.ip138.com, and then the IP address reported to the public Web site.
PS: I used an abandoned Android phone to write a periodic report home IP address of the program, open every day, to their own public network Web server report home IP hehe
Resources
Http://zhidao.baidu.com/link?url=5EAPJZ-y_62ESu4fKYZqiAe19qvKrI9sBc9eeGbW0g8nxu2IikubcSbpzgSVmoEEAq5l7epLNWPcSw9klXS3Sa
Java code for Remote Power on the network