iOS流量监控代码

废话少说,直至贴上我的代码(已经带有单位转换,创建实例类后直接调用getInternetface)

//
//  DetectionFlowHelper.m
//  gooddriver
//
//  Created by comit on 17/3/13.
//  Copyright © 2017年 Comit. All rights reserved.
//

#import "DetectionFlowHelper.h"

#include <ifaddrs.h>
#include <sys/socket.h>
#include <net/if.h>

static DetectionFlowHelper* mDetectionFlow;


@implementation DetectionFlowHelper

/**
 *  获取全局唯一实例
 *
 *  @return
 */
+(id) shareInstance{
    if (!mDetectionFlow) {
        mDetectionFlow = [[DetectionFlowHelper alloc]init];
    }
    return mDetectionFlow;
}

#pragma mark  - 流量监控
- (long long)getInternetface {

    long long hehe = [mDetectionFlow getInterfaceBytes];
    NSLog(@"速率: %@",[self bytesToAvaiUnit:hehe]);
    return hehe;



}



/*获取网络流量信息*/

- (long long) 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;



        /* Not a loopback device. */

        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;

        }

    }

    freeifaddrs(ifa_list);



    NSLog(@"\n[getInterfaceBytes-Total]%d,%d",iBytes,oBytes);

    return iBytes + oBytes;

}


- (NSString *)bytesToAvaiUnit:(long long) bytes
{
    if(bytes < 1024)     // B
    {
        return [NSString stringWithFormat:@"%lldB", bytes];
    }
    else if(bytes >= 1024 && bytes < 1024 * 1024) // KB
    {
        return [NSString stringWithFormat:@"%.1fKB", (double)bytes / 1024];
    }
    else if(bytes >= 1024 * 1024 && bytes < 1024 * 1024 * 1024)   // MB
    {
        return [NSString stringWithFormat:@"%.2fMB", (double)bytes / (1024 * 1024)];
    }
    else    // GB
    {
        return [NSString stringWithFormat:@"%.3fGB", (double)bytes / (1024 * 1024 * 1024)];
    }
}



@end
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
版本的 Process Monitor 增加了多项重要增强功能,包括稳定性和性能改进,强大的过滤选项,修正的进程树对话框(增加了进程存活时间图表),可根据点击位置变换的右键菜单过滤条目,集成带源代码存储的堆栈跟踪对话框,更快的堆栈跟踪,可在 64-位 Windows 上加载 32-位 日志文件的能力等。 Process Monitor 不仅结合了 Filemon(文件监视器) 和 Regmon(注册表监视器) 两个工具的功能,还具有以下一些增强: • 监视进程和线程的启动和退出,包括退出状态代码 • 监视映像 (DLL 和内核模式驱动程序) 加载 • 捕获更多输入输出参数操作 • 非破坏性的过滤器允许你自行定义而不会丢失任何捕获的数据 • 捕获每一个线程操作的堆栈,使得可以在许多情况下识别一个操作的根源 • 可靠捕获进程详细信息,包括映像路径、命令行、完整性、用户和会话ID等等 • 完全可以自定义任何事件的属性列 • 过滤器可以设置为任何数据条件,包括未在当前视图中显示的 • 高级的日志机制,可记录上千万的事件,数GB的日志数据 • 进程树工具显示所有进程的关系 • 原生的日志格式,可将所有数据信息保存,让另一个 Process Monitor 实例加载 • 进程悬停提示,可方便的查看进程信息 • 详细的悬停提示信息让你方便的查看列中不能完整显示的信息 • 搜索可取消 • 系统引导时记录所有操作

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值