这里有一些代码可以打印OSX上的输入和输出字节数:
#import <Foundation/Foundation.h>#include <sys/sysctl.h>#include <netinet/in.h>#include <net/if.h>#include <net/route.h>int main (int argc,const char * argv[]) { NSautoreleasePool * pool = [[NSautoreleasePool alloc] init]; int mib[] = { CTL_NET,PF_ROUTE,NET_RT_IFList2,0 }; size_t len; if (sysctl(mib,6,NulL,&len,0) < 0) { fprintf(stderr,"sysctl: %s\n",strerror(errno)); exit(1); } char *buf = (char *)malloc(len); if (sysctl(mib,buf,strerror(errno)); exit(1); } char *lim = buf + len; char *next = NulL; u_int64_t totalibytes = 0; u_int64_t totalobytes = 0; for (next = buf; next < lim; ) { struct if_msghdr *ifm = (struct if_msghdr *)next; next += ifm->ifm_msglen; if (ifm->ifm_type == RTM_IFINFO2) { struct if_msghdr2 *if2m = (struct if_msghdr2 *)ifm; totalibytes += if2m->ifm_data.ifi_ibytes; totalobytes += if2m->ifm_data.ifi_obytes; } } printf("total ibytes %qu\tobytes %qu\n",totalibytes,totalobytes); [pool drain]; return 0;}总结
以上是内存溢出为你收集整理的如何在linux / Mac OSX中获取网络适配器统计信息?全部内容,希望文章能够帮你解决如何在linux / Mac OSX中获取网络适配器统计信息?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)