int get_mac(char* mac)
linux c获取mac地址
精选 转载{
struct ifreq tmp;
int sock_mac;
char mac_addr[30];
sock_mac = socket(AF_INET, SOCK_STREAM, 0);
if( sock_mac == -1){
perror("create socket fail\n");
return -1;
}
memset(&tmp,0,sizeof(tmp));
strncpy(tmp.ifr_name,"eth0",sizeof(tmp.ifr_name)-1 );
if( (ioctl( sock_mac, SIOCGIFHWADDR, &tmp)) < 0 ){
printf("mac ioctl error\n");
return -1;
}
sprintf(mac_addr, "%02x%02x%02x%02x%02x%02x",
(unsigned char)tmp.ifr_hwaddr.sa_data[0],
(unsigned char)tmp.ifr_hwaddr.sa_data[1],
(unsigned char)tmp.ifr_hwaddr.sa_data[2],
(unsigned char)tmp.ifr_hwaddr.sa_data[3],
(unsigned char)tmp.ifr_hwaddr.sa_data[4],
(unsigned char)tmp.ifr_hwaddr.sa_data[5]
);
printf("local mac:%s\n", mac_addr);
close(sock_mac);
memcpy(mac,mac_addr,strlen(mac_addr));
return true;
}
上一篇:iphone开发-多线程
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
前端如何获取MAC地址
获取硬件mac地址
c++ mac地址