Wake on LAN is a network wake-up function that enables remote start-up. In the lab, there is an FTP server, because it does not start up 24 hours a day, so every time I start the system, I have to run the manual switch, which is very troublesome. So I found some information on the Internet and implemented it myself. In windows, I wrote a simple interface with QT, it will be convenient to open a server later. The principle is that the source host broadcasts a special packet to the target host, provided that the motherboard of the target host supports the wake on LAN and has been set in the BIOS, you need to know the MAC address of the target host. This special packet is called magic packet. It consists of 102 bytes. The first six bytes are 0xff, And the other bytes are the MAC address of the target host (6 bytes are a group, 16 groups in total), as shown in:
After knowing this principle, you can easily implement it. The implementation code is as follows. I will test it myself.
1 # include <stdio. h> 2 # include <ARPA/inet. h> 3 # include <sys/IOCTL. h> 4 # include <net/if. h> 5 # include <string. h> 6 # include <unistd. h> 7 # include <stdlib. h> 8 9 10 int main (INT argc, char * argv []) 11 {12 unsigned char Mac [6] = {0 x, 0x10, 0x20, 0x30, 0x40, 0x50}; // the MAC address of the target host, for example: 00: 10: 20: 30: 40: 5013 unsigned char packet [102]; 14 struct sockaddr_in ADDR; 15 int sockfd, I, j, on = 1; 16 17 // Construct Magic packet18 for (I = 0; I <6; I ++) 19 packet [I] = 0xff; 20 21 for (I = 1; I <17; I ++) 22 for (j = 0; j <6; j ++) 23 packet [I * 6 + J] = Mac [J]; 24 25 // udp26 sockfd = socket (af_inet, sock_dgram, 0); 27 // broadcast 28 setsockopt (sockfd, sol_socket, so_broadcast, & on, sizeof (on )); 29 If (sockfd <0) 30 exit (0); 31 32 memset (void *) & ADDR, 0, sizeof (ADDR); 33 ADDR. sin_family = af_inet; 34 ADDR. sin_port = htons (10000); 35 ADDR. sin_addr.s_addr = inet_addr ("XXX. xxx. xxx. XXX "); // broadcast address 36 37 sendto (sockfd, packet, sizeof (packet), 0, (struct sockaddr *) & ADDR, sizeof (ADDR )); 38 close (sockfd); 39 40 return 0; 41}
You need to modify the MAC address and broadcast address based on the target host.