免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1774 | 回复: 3
打印 上一主题 下一主题

[其他] 哪位兄弟用c/c++写过流量读取 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-04-04 15:30 |只看该作者 |倒序浏览
上层的c/c++能否共享一下:)
多谢了

论坛徽章:
0
2 [报告]
发表于 2014-04-04 15:33 |只看该作者
kkddkkdd11 发表于 2014-04-04 15:30
上层的c/c++能否共享一下:)
多谢了


读/proc/net/dev 或者 类似ifconfig 那个也成
偷个懒 呵呵

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
3 [报告]
发表于 2014-04-07 14:45 |只看该作者
  1. 仅是通过/proc/net/dev读取其中的数据.(转载)
  2. #include
  3. #include
  4. #include
  5. void skipline(FILE *f)
  6. {
  7.   int ch;
  8.   do {
  9.     ch = getc(f);
  10.   } while ( ch != 'n' && ch != EOF );
  11. }
  12. int main(int argc, char *argv[])
  13. {
  14.   FILE *pnd;
  15.   char buffer[BUFSIZ];
  16.   char *interface;
  17.   struct ifinfo {
  18.     char name[8];
  19.     unsigned int r_bytes, r_pkt, r_err, r_drop, r_fifo, r_frame;
  20.     unsigned int r_compr, r_mcast;
  21.     unsigned int x_bytes, x_pkt, x_err, x_drop, x_fifo, x_coll;
  22.     unsigned int x_carrier, x_compr;
  23.   } ifc;
  24.   unsigned long long bin, bout, lbin, lbout;
  25.   int first;
  26.   if ( argc != 2 ) {
  27.     fprintf(stderr, "Usage: %s interfacen", argv[0]);
  28.     exit(1);
  29.   }
  30.   interface = argv[1];
  31.   first = 1;
  32.   lbin = 0; lbout = 0;
  33.   while ( 1 ) {
  34.     pnd = fopen("/proc/net/dev", "r");
  35.     if ( !pnd ) {
  36.       fprintf(stderr, "%s: /proc/net/dev: %s", argv[0], strerror(errno));
  37.       exit(1);
  38.     }
  39.     /* Skip header */
  40.     skipline(pnd);
  41.     skipline(pnd);
  42.     /* Get interface info */
  43.     do {
  44.       if ( fscanf(pnd, " %6[^:]:%u %u %u %u %u %u %u %u %u %u %u %u %u %u %u",
  45.                   &ifc.name,
  46.                   &ifc.r_bytes, &ifc.r_pkt, &ifc.r_err, &ifc.r_drop,
  47.                   &ifc.r_fifo, &ifc.r_frame, &ifc.r_compr, &ifc.r_mcast,
  48.                   &ifc.x_bytes, &ifc.x_pkt, &ifc.x_err, &ifc.x_drop,
  49.                   &ifc.x_fifo, &ifc.x_coll, &ifc.x_carrier, &ifc.x_compr)
  50.            != 16 ) {
  51.         exit(200);
  52.       }
  53.       skipline(pnd);
  54.     } while ( strcmp(ifc.name, interface) );
  55.     bin  = ifc.r_bytes + (lbin & ~0xffffffffULL);
  56.     bout = ifc.x_bytes + (lbout & ~0xffffffffULL);
  57.     if ( bin < lbin )
  58.       bin += (1ULL << 32);
  59.     if ( bout < lbout )
  60.       bout += (1ULL << 32);
  61.     if ( !first ) {
  62.       printf("%d %Lu %Lun", time(NULL), (bout-lbout)*8, (bin-lbin)*8);
  63.       fflush(stdout);
  64.     } else {
  65.       first = 0;
  66.     }
  67.     lbin = bin;  lbout = bout;
  68.     fclose(pnd);
  69.     sleep(1);
  70.   }
  71. }
复制代码

论坛徽章:
0
4 [报告]
发表于 2014-04-08 09:26 |只看该作者
mrpre 发表于 2014-04-07 14:45


谢谢楼上的兄弟,收藏了 :)
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP