局域网远程开机软件制作代码分享
本文导语: 代码如下:/************************************************************************* > File Name: wake_up.c > Author: wz > Created Time: Fri 15 Nov 2013 03:42:47 PM CST ************************************************************************/ #include#include#include#include#include#inclu...
/*************************************************************************
> File Name: wake_up.c
> Author: wz
> Created Time: Fri 15 Nov 2013 03:42:47 PM CST
************************************************************************/
#include
#include
#include
#include
#include
#include
#include
void get_mac(char *argv,unsigned char *save_mac)
{
int j = 0;
int num_x = 0,num_d,first = 1;
while(*argv != 0)
{
if(*argv != ':')
{
if(isdigit(*argv) && first)
{
num_x = (*argv - '0')*16;
first = 0;
argv++;
continue;
}
if(first && isalpha(*argv))
{
num_x = (10+(*argv - 'a'))*16;
first = 0;
argv++;
continue;
}
if(!first && isdigit(*argv))
{
num_x = num_x+(*argv - '0');
first = 1;
argv++;
continue;
}
if(!first && isalpha(*argv))
{
num_x = num_x + (10 + (*argv - 'a'));
first = 1;
argv++;
continue;
}
}
else
{
save_mac[j] = num_x;
num_x = 0;
argv++;
j++;
}
}
save_mac[j] = num_x;
}
int main(int argc,char *argv[])
{
int sockfd,i,j,on = 1,n;
unsigned char packet[120];
unsigned char save_mac[120];
struct sockaddr_in to;
if(argc < 2)
{
fprintf(stderr,"用法:程序名 + 目标主机物理地址");
return 1;
}
get_mac(argv[1],save_mac);
//构建magic数据包
for(i = 0; i < 6; i++)
{
packet[i] = 0xff;
}
for(i = 1; i < 17; i++)
{
memcpy(packet+i*6,save_mac,6);
}
sockfd = socket(AF_INET,SOCK_DGRAM,0);
setsockopt(sockfd,SOL_SOCKET,SO_BROADCAST,&on,sizeof(on));
if(socket < 0)
{
fprintf(stderr,"套接字创建错误n");
return 1;
}
memset((void *)&to,0,sizeof(to));
to.sin_family = AF_INET;
to.sin_port = htons(30000);
to.sin_addr.s_addr = inet_addr("125.217.40.255");
n = sendto(sockfd,packet,sizeof(packet),0,(struct sockaddr *)&to,sizeof(to));
if(n