iOS 直播-网速监控

CXNetworkSpeed.h

 //
// CXNetworkSpeed.h
// CXNetworkSpeedDemo
//
// Created by xubaoaichiyu on 16/08/16.
// Copyright © 2016年 xubaoaichiyu All rights reserved.
// #import <Foundation/Foundation.h> @interface CXNetworkSpeed : NSObject @property (nonatomic, copy, readonly) NSString * receivedNetworkSpeed; @property (nonatomic, copy, readonly) NSString * sendNetworkSpeed; + (instancetype)shareNetworkSpeed; - (void)startMonitoringNetworkSpeed; - (void)stopMonitoringNetworkSpeed; @end /**
* @{@"received":@"100kB/s"}
*/
FOUNDATION_EXTERN NSString *const kNetworkReceivedSpeedNotification; /**
* @{@"send":@"100kB/s"}
*/
FOUNDATION_EXTERN NSString *const kNetworkSendSpeedNotification;

CXNetworkSpeed.m

 //
// CXNetworkSpeed.m
// CXNetworkSpeedDemo
//
// Created by xubaoaichiyu on 16/08/16.
// Copyright © 2016年 xubaoaichiyu All rights reserved.
// #import "CXNetworkSpeed.h"
#include <arpa/inet.h>
#include <net/if.h>
#include <ifaddrs.h>
#include <net/if_dl.h> /**
* @{@"received":@"100kB/s"}
*/
NSString *const kNetworkReceivedSpeedNotification = @"kNetworkReceivedSpeedNotification"; /**
* @{@"send":@"100kB/s"}
*/
NSString *const kNetworkSendSpeedNotification = @"kNetworkSendSpeedNotification"; @interface CXNetworkSpeed ()
{
uint32_t _iBytes;
uint32_t _oBytes;
uint32_t _allFlow;
uint32_t _wifiIBytes;
uint32_t _wifiOBytes;
uint32_t _wifiFlow;
uint32_t _wwanIBytes;
uint32_t _wwanOBytes;
uint32_t _wwanFlow;
} @property (nonatomic, copy) NSString * receivedNetworkSpeed; @property (nonatomic, copy) NSString * sendNetworkSpeed; @property (nonatomic, strong) NSTimer * timer; @end @implementation CXNetworkSpeed static CXNetworkSpeed * instance = nil; + (instancetype)shareNetworkSpeed{
if(instance == nil){
static dispatch_once_t onceToken ;
dispatch_once(&onceToken, ^{
instance = [[self alloc] init] ;
}) ;
}
return instance; } + (instancetype)allocWithZone:(struct _NSZone *)zone{ if(instance == nil){
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{ instance = [super allocWithZone:zone]; });
}
return instance;
} -(instancetype)init{ static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [super init];
_iBytes = _oBytes = _allFlow = _wifiIBytes = _wifiOBytes = _wifiFlow = _wwanIBytes = _wwanOBytes = _wwanFlow = ;
});
return instance; } - (void)startMonitoringNetworkSpeed{
if(_timer)
[self stopMonitoringNetworkSpeed];
_timer = [NSTimer scheduledTimerWithTimeInterval: target:self selector:@selector(netSpeedNotification) userInfo:nil repeats:YES];
} - (void)stopMonitoringNetworkSpeed{
if ([_timer isValid]) {
[_timer invalidate];
}
} - (void)netSpeedNotification{
[self checkNetworkflow];
} -(NSString *)bytesToAvaiUnit:(int)bytes
{
if(bytes < )
{
return [NSString stringWithFormat:@"0KB"];
}
else if(bytes >= && bytes < * ) // KB
{
return [NSString stringWithFormat:@"%.1fKB", (double)bytes / ];
}
else if(bytes >= * && bytes < * * ) // MB
{
return [NSString stringWithFormat:@"%.1fMB", (double)bytes / ( * )];
}
else // GB
{
return [NSString stringWithFormat:@"%.1fGB", (double)bytes / ( * * )];
}
} -(void)checkNetworkflow
{
struct ifaddrs *ifa_list = , *ifa;
if (getifaddrs(&ifa_list) == -)
{
return ;
} uint32_t iBytes = ;
uint32_t oBytes = ;
uint32_t allFlow = ;
uint32_t wifiIBytes = ;
uint32_t wifiOBytes = ;
uint32_t wifiFlow = ;
uint32_t wwanIBytes = ;
uint32_t wwanOBytes = ;
uint32_t wwanFlow = ;
// struct timeval32 time; 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 == )
continue; // network flow
if (strncmp(ifa->ifa_name, "lo", ))
{
struct if_data *if_data = (struct if_data *)ifa->ifa_data;
iBytes += if_data->ifi_ibytes;
oBytes += if_data->ifi_obytes;
allFlow = iBytes + oBytes;
} //wifi flow
if (!strcmp(ifa->ifa_name, "en0"))
{
struct if_data *if_data = (struct if_data *)ifa->ifa_data;
wifiIBytes += if_data->ifi_ibytes;
wifiOBytes += if_data->ifi_obytes;
wifiFlow = wifiIBytes + wifiOBytes;
} //3G and gprs flow
if (!strcmp(ifa->ifa_name, "pdp_ip0"))
{
struct if_data *if_data = (struct if_data *)ifa->ifa_data;
wwanIBytes += if_data->ifi_ibytes;
wwanOBytes += if_data->ifi_obytes;
wwanFlow = wwanIBytes + wwanOBytes;
}
}
freeifaddrs(ifa_list); if (_iBytes != ) {
self.receivedNetworkSpeed = [[self bytesToAvaiUnit:iBytes - _iBytes] stringByAppendingString:@"/s"];
[[NSNotificationCenter defaultCenter] postNotificationName:kNetworkReceivedSpeedNotification object:@{@"received":self.receivedNetworkSpeed}];
} _iBytes = iBytes; if (_oBytes != ) {
self.sendNetworkSpeed = [[self bytesToAvaiUnit:oBytes - _oBytes] stringByAppendingString:@"/s"];
[[NSNotificationCenter defaultCenter] postNotificationName:kNetworkSendSpeedNotification object:@{@"send":self.sendNetworkSpeed}];
}
_oBytes = oBytes;
}
@end

iOS 直播-网速监控的更多相关文章

  1. 网速监控-nload

    用来监控系统网卡实时网速的. 安装 yum install nload -y # 或 apt install nload -y 使用 # 直接运行默认监控第一个网卡, 使用上下方向键来切换网卡. nl ...

  2. shell小脚本--网速监控

    在windows中,我们可以在360等管家软件中显示网速,在linux下想要查看实时的网速怎么办呢?当然在linux下也有很多优秀的软件可以实时显示网络状况!但是在这里我们使用shell脚本来先完成网 ...

  3. Ubuntu 16.04安装基于nethogs衍生的网络监控软件(应用实时网速监控)

    基于nethogs衍生的网络监控软件有如下所列举的: nettop显示数据包类型,按数据包的大小或数量排序. ettercap是以太网的网络嗅探器/拦截器/记录器 darkstat通过主机,协议等方式 ...

  4. ubuntu 16.04网速监控脚本

    #!/bin/bashif [ $# -ne 1 ];thendev="enp2s0"elsedev=$1fi while :doRX1=`/sbin/ifconfig $dev ...

  5. centos 7网速监控脚本

    #!/bin/bashif [ $# -ne 1 ];thendev="eth0"elsedev=$1fi while :doRX1=`/sbin/ifconfig $dev |a ...

  6. linux网络监控_网速测试

    Linux下查看网络即时网速 1.sar命令(一般般) sar -n DEV 1 100 1代表一秒统计并显示一次 100代表统计一百次 sar在sysstat包 2.使用ntop图形工具(没详细用过 ...

  7. iOS开发——实时监控网速(仅作参考,发现一点问题)

    开发中用到获取网速的地方,应该就两种: 1.下载速度,这种可以直接在接受数据的地方统计计算.这个就不讲了. 2.获取手机网卡的数据,可以监控网卡的进出流量,下面就是. #import "Vi ...

  8. Nload(CentOS网速的实时监控)

    Nload(CentOS网速的实时监控)的安装和安装过程中的问题 I. 安装 Download the latest rpmforge-release rpm from wget ftp://ftp. ...

  9. iOS 仿看了吗应用、指南针测网速等常用工具、自定义弹出视图框架、图片裁剪、内容扩展等源码

    iOS精选源码 扩展内容的cell - folding-cell 一个近乎完整的可识别中国身份证信息的Demo 可自动快速... JPImageresizerView 仿微信的图片裁剪 带年月和至今以 ...

随机推荐

  1. 【原创】开源Math.NET基础数学类库使用(11)C#计算相关系数

                   本博客所有文章分类的总目录:[总目录]本博客博文总目录-实时更新  开源Math.NET基础数学类库使用总目录:[目录]开源Math.NET基础数学类库使用总目录 前言 ...

  2. ELF静态链接

    一直对ELF目标文件是怎样链接成可执行文件感到比较的疑惑,ELF文件里面的重定位段是怎样解决符号引用问题的?前几天偶然看了<深入理解计算机系统>里面讲了这个问题,看了之后对里面的实现机制终 ...

  3. MySQL常见错误

    1. TokuFT file system space is really low and access is restricted 解决方法:修改tokudb_fs_reserve_percent参 ...

  4. ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

    为了加强安全性,MySQL5.7为root用户随机生成了一个密码,在error log中,关于error log的位置,如果安装的是RPM包,则默认是/var/log/mysqld.log. 一般可通 ...

  5. 设计数据库字段或者java中使用boolean型时需谨慎

    boolean型变量只有两个值 false和true,我们在设计数据库字段时或者定义java变量时会使用boolean,通常情况下开关类的变量使用无可非议,但请一定要考虑到扩展性. 使用前请仔细考虑一 ...

  6. Oracle 数据库重放(Database Replay)功能演示

    我们可以捕获生产环境的工作量,在测试环境上重放,从而在不影响生产环境的前提下做一些改动测试. 捕获:需要Oracle版本为10.2.0.4或更高. 重放:需要Oracle版本为11g Release ...

  7. 1Z0-053 争议题目解析498

    1Z0-053 争议题目解析498 考试科目:1Z0-053 题库版本:V13.02 题库中原题为: 498.The database Is configured in ARCHIVELOG mode ...

  8. Windows Server 2008 下解析二级域名的方法

    昨天去了客户那里部署网站,用的是客户那边的windows server 2008. 本文主要以总结问题点的形式来说. 问题1:本机的数据库是SQL SERVER 2008R2,客户那边的数据库是SQL ...

  9. 到处都是坑的微信支付V3之 微信支付回调页面

    据上次 到处都是坑的微信支付V3 后很多园友在被虐了千百遍后终于跳转到了亲切的微信支付界面,但输入密码支付后却不知道怎么处理了,接下来补上支付后的处理流程. 1. html中根据前台支付后反馈信息成功 ...

  10. 使用webstom或者idea上传代码到github或coding

    鉴于github网络速度太慢,建议用coding.先介绍github上传方式,因为webstom或idea集成了github,方法简单. git是一个版本控制器,他的作用是管理代码.比如你修改了代码, ...