当前位置: 技术问答>linux和unix
请教:如何在程序中获取本机MAC地址?
来源: 互联网 发布时间:2015-04-11
本文导语: 请赐教,谢谢! | #include #include #include #include #include #include #include #include #include #define ETH_NAME "eth0" int main() { int sock; struct sockaddr_in sin; struct sockaddr sa; struct ifreq ifr; unsi...
请赐教,谢谢!
|
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define ETH_NAME "eth0"
int main()
{
int sock;
struct sockaddr_in sin;
struct sockaddr sa;
struct ifreq ifr;
unsigned char mac[6];
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock == -1)
{
perror("socket");
return -1;
}
strncpy(ifr.ifr_name, ETH_NAME, IFNAMSIZ);
ifr.ifr_name[IFNAMSIZ - 1] = 0;
memset(mac, 0, sizeof(mac));
if (ioctl(sock, SIOCGIFHWADDR, &ifr)
#include
#include
#include
#include
#include
#include
#include
#include
#define ETH_NAME "eth0"
int main()
{
int sock;
struct sockaddr_in sin;
struct sockaddr sa;
struct ifreq ifr;
unsigned char mac[6];
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock == -1)
{
perror("socket");
return -1;
}
strncpy(ifr.ifr_name, ETH_NAME, IFNAMSIZ);
ifr.ifr_name[IFNAMSIZ - 1] = 0;
memset(mac, 0, sizeof(mac));
if (ioctl(sock, SIOCGIFHWADDR, &ifr)