本篇内容主要讲解“如何编写局域网远程开机软件制作代码”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“如何编写局域网远程开机软件制作代码”吧!
代码如下:
#include<unistd.h>
#include<arpa/inet.h>
#include<net/if.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/ioctl.h>
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 <= 0)
{
fprintf(stderr,"发送失败!\n");
return 1;
}
else
{
printf("发送成功.\n");
}
close(sockfd);
return 0;
}
到此,相信大家对“如何编写局域网远程开机软件制作代码”有了更深的了解,不妨来实际操作一番吧!这里是编程网网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!