荔园在线

荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀

[回到开始] [上一篇][下一篇]


发信人: zzt (深切怀念), 信区: Program
标  题: [转载] Re: 如何获得Mac地址(转寄)
发信站: BBS 荔园晨风站 (Wed Apr 26 08:37:40 2000), 转信

【 以下文字转载自 zzt 的信箱 】
【 原文由 zhuzutao.bbs@smth.org 所发表 】
发信人: xcec (就这么样吧), 信区: Linux
标  题: Re: 如何获得Mac地址
发信站: BBS 水木清华站 (Tue Apr 25 10:06:30 2000) WWW-POST

/*      get the hardware address of the interface              */

         int sockfd;
        struct ifreq req;
         unsigned char hwaddr[ETH_ALEN];

        if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
                fputs("socket error!\n", stderr);
                exit (1);
        }

        bcopy("eth0", req.ifr_name, IFNAMSIZ);

        if (ioctl(sockfd, SIOCGIFHWADDR, &req) < 0) {
                fputs("Ioctl error!\n", stderr);
                exit(1);
        }

        bcopy(((struct sockaddr *)&req.ifr_hwaddr)->sa_data, hwaddr, ETH_ALEN);
         close(sockfd);

/*       Print the MAC address in xx:xx:xx:xx:xx:xx format    */
         printf("%02x:%02x:%02x:%02x:%02x:%02x\n", *hwaddr, *(hwaddr+1),
                 *(hwaddr+2), *(hwaddr+3), *(hwaddr+4), *(hwaddr+5));

【 在 xgh (小丙) 的大作中提到: 】
: 是否使用
: ioctl(sockfd, SIOCGIFHWADDR, &nowreq);
: (之前sockfd用socket函数初始化了,但nowreq我就没动过它)
: 然后应该nowreq.ifr_hwaddr里面就是需要的东西了,但是
: 里面是些希奇古怪的东西。不明白是否是网络顺序的地址。
: 在linux如何看?

--
欢迎光临中国经济学 http://xiachun.dhs.org/~xiachun

经济学资源下载: ftp://xiachun.dhs.org/pub

※ 来源:·BBS 水木清华站 smth.org·[FROM: 162.105.33.162]
--
※ 转载:·BBS 荔园晨风站 bbs.szu.edu.cn·[FROM: 192.168.1.11]


[回到开始] [上一篇][下一篇]

荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店