描述
部分内容基于C#,本文简述UDP,WOL包和ECHO包的基本用法和包结构。
包参数和终结点
长度为 102 bytes
发送到 IPAddress.Broadcast:7 (ECHO)
发送到 IPAddress.Broadcast:40000 (WOL)
包内容
例:您要操作的MAC为04-0E-3C-2C-80-A3
内容:包头6个0xFF,后循环16次唤醒主机的MAC地址。
ff ff ff ff ff ff
04 0e 3c 2c 80 a3 04 0e 3c 2c 80 a3 04 0e 3c 2c 80 a3 04 0e 3c 2c 80 a3
04 0e 3c 2c 80 a3 04 0e 3c 2c 80 a3 04 0e 3c 2c 80 a3 04 0e 3c 2c 80 a3
04 0e 3c 2c 80 a3 04 0e 3c 2c 80 a3 04 0e 3c 2c 80 a3 04 0e 3c 2c 80 a3
04 0e 3c 2c 80 a3 04 0e 3c 2c 80 a3 04 0e 3c 2c 80 a3 04 0e 3c 2c 80 a3
代码示例
组建包
string str_mac = "04-0E-3C-2C-80-A3";
string[] split = str_mac.Split(str_mac.Contains(":") ? ':' : '-');
byte[] mac = new byte[6];
byte[] packet = new byte[17 * 6]; //102 Bytes
for (var i = 0; i < 6; i++) {
mac[i] = Convert.ToByte(split[i], 16); //String格式化为字节
}
for (int i = 0; i < 6; i++) {
packet[i] = 0xff;
}
for (int i = 1; i <= 16; i++) { //添加16次MAC到包内
for (int j = 0; j < 6; j++) {
packet[i * 6 + j] = mac[j];
}
}
发送包
UdpClient client = new UdpClient();
client.Connect(IPAddress.Broadcast, 40000);
for (int a = 0; a < 10; a++) {
client.Send(packet, packet.Length);
}
client.Close();
client = new UdpClient();
client.Connect(IPAddress.Broadcast, 7);
for (int b = 0; b < 10; b++) {
client.Send(packet, packet.Length);
}
client.Close();