// http://sourceforge.net/projects/wakeonlan/
// WakeOnLan - commandline application to start computers via wake on lan enbaled
// network interface card.
// Author: erik.tornstam@home.se
// Copyright: erik.tornstam@home.se#include "stdio.h"
#include "iostream.h"
#include "winsock2.h"#define DEFAULT_BROADCAST "255.255.255.255"
#define DEFAULT_PORT 32767parse_mac(unsigned char *mac, char *str)
{
int i;
int count;
char c;
unsigned char val;
int separator_ok = 1;for (i = 0; i < 6; i++) {
mac[i] = 0;
}for (i = 0; i < 6; i++) {
count = 0;
val = 0;
do {
c = toupper(*str++);
if (c >= '0' && c <= '9')
{
val = (val * 16) + (c - '0');
}
else if (c >= 'A' && c <= 'F')
{
val = (val * 16) + (c - 'A') + 10;
}
else if (c == '-' || c == ':')
{
if (separator_ok || count-- != 0)
break;
}
else if (c == '\0')
{
str--;
break;
}
else
{
return 0;
}
separator_ok=1;
} while (++count < 2);separator_ok=(count<2);
*mac++ = val;
}if (*str)
return 0;return 1;
}int wakeup(char * sMacAddr, char * sBroadcast, int iPort)
{
WSADATA wsaData;
int s;
struct sockaddr_in bcast;
char msg[1024];
int msglen = 0;
unsigned char macaddr[6];if (! parse_mac(macaddr, sMacAddr) )
{
cerr << "Illegal MAC address" << endl;
return -1;
}for (int i = 0; i < 6; i++) {
msg[msglen++] = (char)0xff;
}for (i = 0; i < 16; i++) {
for (int j = 0; j < sizeof(macaddr); j++) {
msg[msglen++] = macaddr[j];
}
}if (!WSAStartup(MAKEWORD(1, 1), &wsaData))
{
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) != INVALID_SOCKET)
{
memset((char *)&bcast,0,sizeof(bcast));
bcast.sin_family=AF_INET;
bcast.sin_addr.s_addr=inet_addr(sBroadcast);
bcast.sin_port=htons(iPort);if ( connect(s,(struct sockaddr *)&bcast,16)== -1)
{
cerr << "Cannot connect socket" << endl;
return -2;
}sendto(s, (const char *)&msg, msglen, 0, (struct sockaddr *)&bcast, sizeof(bcast));
closesocket(s);
}
else
{
cerr << "Error getting socket" << endl;
}WSACleanup();
return(0);
}
else
return -3;
}void printHelp(char * argv[])
{
cout << "WakeOnLan (c) erik.tornstam@home.se" << endl;
cout << "Sends a WakeOnLan packet to a network." << endl << endl;
cout << "Usage: " << argv[0] << " -a macaddress [-b broadcast address] [-p port] " << endl << endl;
cout << "\t-a\tMAC-address of NIC to wake. (00-04-00-c8-85-2d)" << endl;
cout << "\t-b\tBroadcast address, default address is " << DEFAULT_BROADCAST << endl;
cout << "\t-p\tPort number, default port is " << DEFAULT_PORT << endl;
}int main(int argc, char* argv[])
{
char macaddress[20] = "";
char broadcast[20] = DEFAULT_BROADCAST;
int port = DEFAULT_PORT;
int argcnt=0;for(int c=1; c
{
if(stricmp("-a",argv[c])==0 && (argc > c+1) && argv[c+1][0]!='-')
{
strcpy(macaddress,argv[c+1]);
argcnt++;
}
if(stricmp("-b",argv[c])==0 && (argc > c+1) && argv[c+1][0]!='-')
{
strcpy(broadcast,argv[c+1]);
argcnt++;
}
if(stricmp("-p",argv[c])==0 && (argc > c+1) && argv[c+1][0]!='-')
{
port=atoi(argv[c+1]);
argcnt++;
}
}if(argcnt < 1 || strcmp(macaddress,"")==0)
{
printHelp(argv);
return -1;
}
cout << "Wakeupcall to " << macaddress << " broadcast over " << broadcast << " on port " << port << endl;
wakeup(macaddress, broadcast, port);return 0;
출처 : http://visitagain.tistory.com/tag/WOL
}
VS2005 C++로 컴파일 해봤음, 오류남. ..
'Programming' 카테고리의 다른 글
11월 17일 랩실 C언어 교육 (0) | 2009.11.17 |
---|---|
WOL c++ 다른 소스코드 (0) | 2009.10.29 |
IE8설치 후 VS에러 패치 (0) | 2009.10.14 |
컴포넌트의 종류 (0) | 2009.08.28 |
Flex Builder 3 (0) | 2009.08.28 |