今天一直在学习代码论坛之类的问题,上午正好有机会和大家讨论一下.
废话少说直接上代码
bool GetMac(char *pBuffer) { struct ifreq *ifr; struct ifconf ifc; int s, i; int numif; // find number of interfaces. memset(&ifc, 0, sizeof(ifc)); ifc.ifc_ifcu.ifcu_req = NULL; ifc.ifc_len = 0; if ((s = ::socket(PF_INET, SOCK_STREAM, 0)) < 0) { CCLog("SgProfile::GetMac 111111"); return false; } if (ioctl(s, SIOCGIFCONF, &ifc) < 0) { CCLog("SgProfile::GetMac 222222"); return false; } if ((ifr = (ifreq*) malloc(ifc.ifc_len)) == NULL) { CCLog("SgProfile::GetMac 333333"); return false; } ifc.ifc_ifcu.ifcu_req = ifr; if (ioctl(s, SIOCGIFCONF, &ifc) < 0) { CCLog("SgProfile::GetMac 333333 ioctl SIOCGIFCONF error!"); return false; } numif = ifc.ifc_len / sizeof(struct ifreq); for (i = 0; i < numif; i++) { struct ifreq *r = &ifr[i]; struct sockaddr_in *sin = (struct sockaddr_in*)&r->ifr_addr; if (!strcmp(r->ifr_name, "lo")) continue; // skip loopback interface // get MAC address if(ioctl(s, SIOCGIFHWADDR, r) < 0) { CCLog("SgProfile::GetMac 333333 ioctl(SIOCGIFHWADDR) error!"); continue; } memcpy(pBuffer,r->ifr_hwaddr.sa_data,6); close(s); free(ifr); return true; } return false; }
在国外论坛里头找的,写在这里以备忘!
文章结束给大家分享下程序员的一些笑话语录: 某程序员对书法十分感兴趣,退休后决定在这方面有所建树。花重金购买了上等的文房四宝。一日突生雅兴,一番磨墨拟纸,并点上了上好的檀香,颇有王羲之风 范,又具颜真卿气势,定神片刻,泼墨挥毫,郑重地写下一行字:hello world.