zoukankan      html  css  js  c++  java
  • iOS开发——实时监控网速(仅作参考,发现一点问题)

      开发中用到获取网速的地方,应该就两种:

      1、下载速度,这种可以直接在接受数据的地方统计计算。这个就不讲了。
      2、获取手机网卡的数据,可以监控网卡的进出流量,下面就是。

    #import "ViewController.h"

    #include <ifaddrs.h>

    #include <arpa/inet.h>

    #include <net/if.h>

    @interface ViewController ()

    @end

    @implementation ViewController

    - (void)viewDidLoad {

        [super viewDidLoad];

            

        NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(getInternetface) userInfo:nil repeats:YES];

        [timer fireDate];

    }

    - (void)getInternetface {

        long long hehe = [self getInterfaceBytes];

        

        NSLog(@"hehe:%lld",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(@" [getInterfaceBytes-Total]%d,%d",iBytes,oBytes);

        return iBytes + oBytes;

    }

    @end

  • 相关阅读:
    Nacos微服务部署(超详细)基于Centos7
    Centos7配置Mysql5.7数据库
    django搭建web (一)
    NetFPGA-1G-CML Demo --- reference_router_nf1_cml
    Linux下Java通用安装方法
    NetFPGA-1G-CML Demo --- openflow_switch
    原型设计(结对第一次)
    第二次作业——个人项目实战
    游戏
    python学习笔记-问题
  • 原文地址:https://www.cnblogs.com/yyt-hehe-yyt/p/5954009.html
Copyright © 2011-2022 走看看