如何判断流量是GPRS 3G还是Wifi产生的?

发布于 2024-12-05 07:30:20 字数 55 浏览 2 评论 0原文

在流量监控程序中,如何判断流量是由GPRS还是Wifi产生的? 请给我一些想法和建议?多谢 。

In the flow monitoring program, how to determine the flow is generated by GPRS or Wifi ?
Please provide me some ideas and suggestions? Thanks a lot .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少跟Wǒ拽 2024-12-12 07:30:20

您可以通过以下方式检查 wifi 是否已连接

ConnectivityManager conman = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
boolean wifi = conman.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected();

。可以通过更改 TYPE_WIFI 属性来检查其他网络连接类型。

You can check to see if wifi is connected by the following

ConnectivityManager conman = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
boolean wifi = conman.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected();

Checking for other network connection types can be done by changing the TYPE_WIFI property.

罪歌 2024-12-12 07:30:20

哦,我明白了!
通过读取 /proc/self/net/dev 或 /proc/net/dev 来获取应用程序流量统计信息。

void skipline(FILE *f) 
{ 
  int ch; 
  do { 
    ch = getc(f); 
  } while ( ch != 'n' && ch != EOF ); 
}

int main(int argc, char *argv[]) 
{ 
  FILE *pnd; 
  char buffer[BUFSIZ]; 
  char *interface; 
  struct ifinfo { 
    char name[8]; 
    unsigned int r_bytes, r_pkt, r_err, r_drop, r_fifo, r_frame; 
    unsigned int r_compr, r_mcast; 
    unsigned int x_bytes, x_pkt, x_err, x_drop, x_fifo, x_coll; 
    unsigned int x_carrier, x_compr; 
  } ifc; 
  unsigned long long bin, bout, lbin, lbout; 
  int first;

  if ( argc != 2 ) { 
    fprintf(stderr, "Usage: %s interfacen", argv[0]); 
    exit(1); 
  }

  interface = argv[1];

  first = 1; 
  lbin = 0; lbout = 0;

  while ( 1 ) { 
    pnd = fopen("/proc/net/dev", "r"); 
    if ( !pnd ) { 
      fprintf(stderr, "%s: /proc/net/dev: %s", argv[0], strerror(errno)); 
      exit(1); 
    }

    /* Skip header */ 
    skipline(pnd); 
    skipline(pnd);

    /* Get interface info */ 
    do { 
      if ( fscanf(pnd, " %6[^:]:%u %u %u %u %u %u %u %u %u %u %u %u %u %u %u", 
                  &ifc.name, 
                  &ifc.r_bytes, &ifc.r_pkt, &ifc.r_err, &ifc.r_drop, 
                  &ifc.r_fifo, &ifc.r_frame, &ifc.r_compr, &ifc.r_mcast, 
                  &ifc.x_bytes, &ifc.x_pkt, &ifc.x_err, &ifc.x_drop, 
                  &ifc.x_fifo, &ifc.x_coll, &ifc.x_carrier, &ifc.x_compr) 
           != 16 ) { 
        exit(200); 
      } 
      skipline(pnd); 
    } while ( strcmp(ifc.name, interface) );

    bin  = ifc.r_bytes + (lbin & ~0xffffffffULL); 
    bout = ifc.x_bytes + (lbout & ~0xffffffffULL);

    if ( bin < lbin ) 
      bin += (1ULL << 32); 
    if ( bout < lbout ) 
      bout += (1ULL << 32);

    if ( !first ) { 
      printf("%d %Lu %Lun", time(NULL), (bout-lbout)*8, (bin-lbin)*8); 
      fflush(stdout); 
    } else { 
      first = 0; 
    }

    lbin = bin;  lbout = bout;

    fclose(pnd);

    sleep(1); 
  } 
}

Oh,I got it!
Through reading /proc/self/net/dev or /proc/net/dev to get the application flow statistics .

void skipline(FILE *f) 
{ 
  int ch; 
  do { 
    ch = getc(f); 
  } while ( ch != 'n' && ch != EOF ); 
}

int main(int argc, char *argv[]) 
{ 
  FILE *pnd; 
  char buffer[BUFSIZ]; 
  char *interface; 
  struct ifinfo { 
    char name[8]; 
    unsigned int r_bytes, r_pkt, r_err, r_drop, r_fifo, r_frame; 
    unsigned int r_compr, r_mcast; 
    unsigned int x_bytes, x_pkt, x_err, x_drop, x_fifo, x_coll; 
    unsigned int x_carrier, x_compr; 
  } ifc; 
  unsigned long long bin, bout, lbin, lbout; 
  int first;

  if ( argc != 2 ) { 
    fprintf(stderr, "Usage: %s interfacen", argv[0]); 
    exit(1); 
  }

  interface = argv[1];

  first = 1; 
  lbin = 0; lbout = 0;

  while ( 1 ) { 
    pnd = fopen("/proc/net/dev", "r"); 
    if ( !pnd ) { 
      fprintf(stderr, "%s: /proc/net/dev: %s", argv[0], strerror(errno)); 
      exit(1); 
    }

    /* Skip header */ 
    skipline(pnd); 
    skipline(pnd);

    /* Get interface info */ 
    do { 
      if ( fscanf(pnd, " %6[^:]:%u %u %u %u %u %u %u %u %u %u %u %u %u %u %u", 
                  &ifc.name, 
                  &ifc.r_bytes, &ifc.r_pkt, &ifc.r_err, &ifc.r_drop, 
                  &ifc.r_fifo, &ifc.r_frame, &ifc.r_compr, &ifc.r_mcast, 
                  &ifc.x_bytes, &ifc.x_pkt, &ifc.x_err, &ifc.x_drop, 
                  &ifc.x_fifo, &ifc.x_coll, &ifc.x_carrier, &ifc.x_compr) 
           != 16 ) { 
        exit(200); 
      } 
      skipline(pnd); 
    } while ( strcmp(ifc.name, interface) );

    bin  = ifc.r_bytes + (lbin & ~0xffffffffULL); 
    bout = ifc.x_bytes + (lbout & ~0xffffffffULL);

    if ( bin < lbin ) 
      bin += (1ULL << 32); 
    if ( bout < lbout ) 
      bout += (1ULL << 32);

    if ( !first ) { 
      printf("%d %Lu %Lun", time(NULL), (bout-lbout)*8, (bin-lbin)*8); 
      fflush(stdout); 
    } else { 
      first = 0; 
    }

    lbin = bin;  lbout = bout;

    fclose(pnd);

    sleep(1); 
  } 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文