In the previous article "remote host switch program", we talked about how to use the wake on LAN protocol to enable the boot function. But No details are provided.
"Wake on LAN" is a function currently supported by most wired network cards. Currently, the wireless network card of the notebook is not available, it is mainly because enabling this function will still consume a little power when it is turned off, but with the development of low-power hardware and high-capacity battery, it is easy to enable this function for the notebook.
To enable the "Wake on LAN" function, you need to set it in the power options in the BIOS of the motherboard. Remember that after this function is enabled, the NIC will consume a little power even if it is turned off. After the network card is shut down, it will continue to accept packets from the network. when it receives the boot command, it will start the computer. What is the format of this boot command?
6 * 0xff + 16 * MAC address
Repeated six times 0xff and the MAC address of the network card to be started six times.
A 0xff is 8 bits, a MAC address is 48 bits, and 6*8 + 16*48 = 816 bits = 102 bits.
To activate a host, you need to send a 102-byte packet to the NIC. If C # is used, it must be defined as follows:
Byte [] sendbytes = new byte [1, 102];
The first six bytes of the data packet are the same no matter which server is activated or which is 0xff. If C # is used:
For (INT I = 0; I <6; I ++)
{
Sendbytes [I] = 0xff;
}
Then fill in the MAC address. Let's assume that the MAC address of the target machine's Nic is:
08-00-27-00-d8-9a
Process the input Mac.
String macaddress = "08-00-27-00-d8-9a ";
Macaddress = macaddress. Replace (":", ""). Replace ("-","");
Of course, you need to process this string to convert it to the byte type. If you use C #, you can use the byte. parse () method to convert the string to the byte type. For example:
Byte test = byte. parse ("FF", system. Globalization. numberstyles. hexnumber );
You can convert the string "FF" to 0b1111111111. Then convert the above MAC address string into a byte array, that is, each time you take two strings into one byte.
For (INT I = 0; I <16; I ++)
{
For (Int J = 0; j <6; j ++)
{
Sendbytes [(I + 1) * 6 + J] = byte. parse (macaddress. substring (J * 2, 2), system. Globalization. numberstyles. hexnumber );
}
}
Because it needs to be repeated 16 times, a for loop is nested in the previous layer. After the above, a 102-byte boot packet is ready. The next step is to send it out.
You can use UDP to send messages to port 7 or port 9. The address to which the IP address is sent, as long as the target machine can receive it. Generally, it is sent in the form of a broadcast package (255.255.255.255 is the broadcast address) in the LAN ).
Udpclient = new udpclient ();
Udpclient. Send (sendbytes, sendbytes. length, "00000000255", 7 );
This completes a simple Remote Boot function.
Let me talk about the destination address. Sending a broadcast packet in the LAN allows all machines in the network to receive the packet. Can the WAN function be enabled? This is fine, as long as the computer on the target host can receive this packet. Assume that you have a computer outside the home and use ADSL Broadband. The computer is connected to the route, and the route is connected to the ADSL dial-up Internet. As long as the route is not off and the ADSL is not off, you have an IP address. When you send data to this IP address over the Internet, it will be received by the route (of course you cannot reach your machine). At this time, you need to set route ing so that data sent from the Internet to route port 7 is forwarded to your machine, then your machine will be able to receive this packet.
For more information about wake on LAN, you must read the professional documentation. This code has been provided in the previous article.