// 局域网远程唤醒Wakeup On LAN) — 发送一个Magic Packet到某个MAC地址
// Magic Packet:UDP广播包,端口不限,数据是FF-FF-FF-FF-FF-FF加16个MAC
// 编译:cl sendmagic.cpp ws2_32.lib
#include <windows.h>
#include <stdio.h>
int mainint argc, char *argv[])
{
//检查命令行参数
ifargc!=2)
{
printf"Wakeup your PC on LAN! by CJ<4uto@163.com>/n");
printf"Usage: %s <MAC address>/n", argv[0]);
printf"Example: %s 00-D0-4C-BF-52-BA", argv[0]);
return 0;
}
//检查MAC地址是否正确
for char * a=argv[1]; *a; a++)
if *a!='-' && !isxdigit*a))
{
fprintfstderr, "MAC Adresse must be like this: 00-D0-4C-BF-52-BA");
return 1;
}
int dstaddr[6];
int i=sscanfargv[1], "%2x-%2x-%2x-%2x-%2x-%2x",
&dstaddr[0], &dstaddr[1], &dstaddr[2], &dstaddr[3], &dstaddr[4], &dstaddr[5]);
if i!=6)
{
fprintfstderr, "Invalid MAC Adresse!");
return 1;
}
unsigned char ether_addr[6];
for i=0; i<6; i++)
ether_addr[i]=dstaddr[i];
//构造Magic Packet
u_char magicpacket[200];
memsetmagicpacket, 0xff, 6);
int packetsize=6;
for i=0; i<16; i++)
{
memcpymagicpacket+packetsize, ether_addr, 6);
packetsize+=6;
}
//启动WSA
WSADATA WSAData;
if WSAStartup MAKEWORD2, 0), &WSAData)!=0)
{
fprintfstderr, "WSAStartup failed: %d/n", GetLastError));
return 1;
}
//创建socket
SOCKET sock=socketAF_INET, SOCK_DGRAM, 0);
if sock==INVALID_SOCKET)
{
fprintfstderr, "Socket create error: %d/n", GetLastError));
return 1;
}
//设置为广播发送
BOOL bOptVal=TRUE;
int iOptLen=sizeofBOOL);
if setsockoptsock, SOL_SOCKET, SO_BROADCAST, char*)&bOptVal, iOptLen)==SOCKET_ERROR)
{
fprintfstderr, "setsockopt error: %d/n", WSAGetLastError));
closesocketsock);
WSACleanup);
return 1;
}
sockaddr_in to;
to.sin_family=AF_INET;
to.sin_port=htons0);
to.sin_addr.s_addr=htonlINADDR_BROADCAST);
//发送Magic Packet
if sendtosock, const char *)magicpacket, packetsize, 0, const struct sockaddr *)&to, sizeofto))==SOCKET_ERROR)
fprintfstderr, "Magic packet send error: %d", WSAGetLastError));
else
printf"Magic packet send!");
closesocketsock);
WSACleanup);
return 0;
}
转载于:https://my.oschina.net/XTechFly/blog/639640