1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
| #include <stdio.h> #include <stdlib.h> #include <string.h> #include <WinSock2.h> #include<iostream> #include<fstream> using namespace std;
const int maxn=1005; struct node{ int id; string name; unsigned char mac[6]; int port; }mach[maxn]; int choose,tot=0; hostent *host; string s;
void init(){ ifstream ifs; ifs.open("save.bak0", ios::in); int temp; ifs >>s; ifs >>tot; for(int i = 0;i<tot;i++){ ifs >>mach[i].name; ifs >>mach[i].id; for(int j = 0;j<=5;j++){ ifs >>temp; mach[i].mac[j]=temp; } ifs >>mach[i].port; } ifs.close(); } void save(){ freopen("save.bak0","w",stdout); cout<<s<<endl<<endl; cout<<tot<<endl<<endl; for(int i = 0;i<tot;i++){ cout<<mach[i].name<<endl; cout<<mach[i].id<<endl; cout<<(int)mach[i].mac[0]<<endl; cout<<(int)mach[i].mac[1]<<endl; cout<<(int)mach[i].mac[2]<<endl; cout<<(int)mach[i].mac[3]<<endl; cout<<(int)mach[i].mac[4]<<endl; cout<<(int)mach[i].mac[5]<<endl; cout<<mach[i].port<<endl<<endl; } fclose(stdin); }
bool wol(int which){ unsigned char packet[102]; struct sockaddr_in addr; int sockfd, i,j, on = 1;
for(i=0;i<6;i++){ packet[i] = 0xFF; } for(i=1;i<17;i++){ for(j=0;j<6;j++){ packet[i*6+j] = mach[which].mac[j]; } }
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST,(char*)&on, sizeof(on)); if(sockfd < 0){ return false; } memset((void*)&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(mach[which].port); addr.sin_addr.s_addr = inet_addr(inet_ntoa(*(struct in_addr *)host->h_addr_list[0])); sendto(sockfd,(char*)&packet, sizeof(packet), 0, (struct sockaddr *)&addr, sizeof(addr)); return true; } int main(){ init(); printf("Welcome back, Better_OIer!\n\n"); printf("Checking Server IP..."); WSADATA wsaData; WSAStartup(MAKEWORD(2, 2), &wsaData); host = gethostbyname(s.c_str()); while(!host) { printf("\nGet IP address error!\n"); printf("Change server name?(y/n):(Default=N) "); char flag; cin>>flag; if(flag!='y'&&flag!='Y')goto en; printf("Enter your new server name: "); cin>>s; host = gethostbyname(s.c_str()); } printf("Successful!\n"); printf("Address type: %s\n", (host->h_addrtype == AF_INET) ? "AF_INET" : "AF_INET6"); printf("IP address: %s\n\n", inet_ntoa(*(struct in_addr *)host->h_addr_list[0])); printf("Choose machines you want to wake up:\n"); for(int i = 0;i<tot;i++){ printf("%3d %s\n",mach[i].id,mach[i].name.c_str()); } printf("Enter 1 to %d:(0 to wake up all the %d machine): ",tot,tot); cin>>choose; puts(""); if(choose==0){ printf("Waking up all the machine...\n"); for(int i = 0;i<tot;i++){ if(wol(i)){ printf("Successfully wake up %s(id=%d)!!!\n",mach[i].name.c_str(),mach[i].id); }else{ printf("Error when waking up %s(id=%d)!!!\n",mach[i].name.c_str(),mach[i].id); } } } else if(choose == 'q'||choose=='Q')goto en; else{ if(wol(--choose)){ printf("Successfully wake up %s(id=%d)!!!\n",mach[choose].name.c_str(),mach[choose].id); }else{ printf("Error when waking up %s(id=%d)!!!\n",mach[choose].name.c_str(),mach[choose].id); } } en: printf("\nQuit in 3s..."); save(); Sleep(3000); return 0; }
|