原文地址:http://hankjin.blog.163.com/blog/static/3373193720091112101516694/
网络唤醒主要需要主板支持, 在BIOS里设置Wake on lan[Enable]
网络唤醒的包很容易构建,但是在网管服务器上始终不能工作,刚开始以为是程序的问题, 后来发现Ubuntu里有一个wakeonlan的perl脚本,在ubuntu上工作正常,而在网关上却不能正常工作 最后发现是好像是双网卡的问题,给wakeonlan加上一个-i参数指定内网的ip,启动成功! sudo apt-get install wakeonlan #实际就是在/usr/bin下生成一个名为wakeonlan的perl脚本,拷贝到Fedora等也可以用。 wakeonlan -i 192.201.0.255 00:1c:25:db:bd:46
贴上一个c语言的网络唤醒代码 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #define oops(msg) {perror(msg); exit(1);}
int main(int argc, char* argv[]){ char mac[6]; char packet[102]; struct sockaddr_in addr; int sockid, i, on = 1; char * p = "00:1c:25:db:bd:46";/* destination host ip address*/ sscanf(p, "%2x:%2x:%2x:%2x:%2x:%2x", mac, mac+1, mac+2, mac+3, mac+4, mac+5); memset(packet, 0xff, 6); for(i = 0; i < 16; i ++){ memcpy(packet + 6*(i+1), mac, sizeof(mac)); } sockid = socket(AF_INET, SOCK_DGRAM, 0); setsockopt(sockid, SOL_SOCKET, SO_BROADCAST,&on, sizeof(on)); if(sockid == -1) oops("socket"); memset((void*)&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(10000); addr.sin_addr.s_addr = inet_addr("192.201.0.255"); sendto(sockid, packet, sizeof(packet), 0, (struct sockaddr *)&addr, sizeof(addr)); close(sockid);
return 0; }
贴上一个c语言的网络唤醒代码 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #define oops(msg) {perror(msg); exit(1);}
int main(int argc, char* argv[]){ char mac[6]; char packet[102]; struct sockaddr_in addr; int sockid, i, on = 1; char * p = "00:1c:25:db:bd:46";/* destination host ip address*/ sscanf(p, "%2x:%2x:%2x:%2x:%2x:%2x", mac, mac+1, mac+2, mac+3, mac+4, mac+5); memset(packet, 0xff, 6); for(i = 0; i < 16; i ++){ memcpy(packet + 6*(i+1), mac, sizeof(mac)); } sockid = socket(AF_INET, SOCK_DGRAM, 0); setsockopt(sockid, SOL_SOCKET, SO_BROADCAST,&on, sizeof(on)); if(sockid == -1) oops("socket"); memset((void*)&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(10000); addr.sin_addr.s_addr = inet_addr("192.201.0.255"); sendto(sockid, packet, sizeof(packet), 0, (struct sockaddr *)&addr, sizeof(addr)); close(sockid);
return 0; }