zoukankan      html  css  js  c++  java
  • wifi 3G 流量

    //

    //  flowStatis.c

    //  Test

    //

    //  Created by iXcoder on 12-7-19.

    //  Copyright (c) 2012 iXcoder. All rights reserved.

    //

    #import <Foundation/Foundation.h>

    #include <stdio.h>

    #include <ifaddrs.h>

    #include <sys/socket.h>

    #include <net/if.h>

    //3G流量统计

    int getGprs3GFlowIOBytes() {

        

        struct ifaddrs *ifa_list= 0, *ifa;

        if (getifaddrs(&ifa_list)== -1) {

            return 0;

        }

        uint32_t iBytes = 0;

        uint32_t oBytes = 0;

        for (ifa = ifa_list; ifa; ifa = ifa->ifa_next) {

            if (AF_LINK != ifa->ifa_addr->sa_family)

                continue;

            if (!(ifa->ifa_flags& IFF_UP) &&!(ifa->ifa_flags & IFF_RUNNING))

                continue;

            if (ifa->ifa_data == 0)

                continue;

            if (!strcmp(ifa->ifa_name,"pdp_ip0")) {

                struct if_data *if_data = (struct if_data*)ifa->ifa_data;

                iBytes += if_data->ifi_ibytes;

                oBytes += if_data->ifi_obytes;

    NSLog(@"%s :iBytes is %d, oBytes is %d",ifa->ifa_name, iBytes, oBytes);

            }

        }

        freeifaddrs(ifa_list);

        return iBytes + oBytes;

    }

    //Wifi流量统计

    - (long long int)getInterfaceBytes {

        

        struct ifaddrs *ifa_list = 0, *ifa;

        if (getifaddrs(&ifa_list) == -1) {

            return 0;

        }

        uint32_t iBytes = 0;

        uint32_t oBytes = 0;

        for (ifa = ifa_list; ifa; ifa = ifa->ifa_next) {

            if (AF_LINK != ifa->ifa_addr->sa_family)

                continue;

            if (!(ifa->ifa_flags & IFF_UP) && !(ifa->ifa_flags & IFF_RUNNING))

                continue;

            if (ifa->ifa_data == 0)

                continue;

            if (strncmp(ifa->ifa_name, "lo"2)) {

                struct if_data *if_data = (struct if_data *)ifa->ifa_data;

                iBytes += if_data->ifi_ibytes;

                oBytes += if_data->ifi_obytes;

                NSLog(@"%s :iBytes is %d, oBytes is %d", ifa->ifa_name, iBytes, oBytes);

            }

        }

        freeifaddrs(ifa_list);

        return iBytes+oBytes;

    }

    将流量值写入文件,或SQLite,或使用CoreData,方便按天、周、月统计。

  • 相关阅读:
    android_SurfaceView 画图
    android_activity_研究(二)
    android_sdcard读写(一)
    双缓冲的小程序
    C++运算符重载小程序
    再练
    菜鸟的第一个博客(用java写的个小文本框)
    LUCENE第一个小程序(有错)
    蜗牛在奔跑
    指定目录下的java文件存储到一个Txt文本文件中
  • 原文地址:https://www.cnblogs.com/qq378829867/p/3777833.html
Copyright © 2011-2022 走看看