iOS 直播-网速监控
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 直播-网速监控的更多相关文章
- 网速监控-nload
用来监控系统网卡实时网速的. 安装 yum install nload -y # 或 apt install nload -y 使用 # 直接运行默认监控第一个网卡, 使用上下方向键来切换网卡. nl ...
- shell小脚本--网速监控
在windows中,我们可以在360等管家软件中显示网速,在linux下想要查看实时的网速怎么办呢?当然在linux下也有很多优秀的软件可以实时显示网络状况!但是在这里我们使用shell脚本来先完成网 ...
- Ubuntu 16.04安装基于nethogs衍生的网络监控软件(应用实时网速监控)
基于nethogs衍生的网络监控软件有如下所列举的: nettop显示数据包类型,按数据包的大小或数量排序. ettercap是以太网的网络嗅探器/拦截器/记录器 darkstat通过主机,协议等方式 ...
- ubuntu 16.04网速监控脚本
#!/bin/bashif [ $# -ne 1 ];thendev="enp2s0"elsedev=$1fi while :doRX1=`/sbin/ifconfig $dev ...
- centos 7网速监控脚本
#!/bin/bashif [ $# -ne 1 ];thendev="eth0"elsedev=$1fi while :doRX1=`/sbin/ifconfig $dev |a ...
- linux网络监控_网速测试
Linux下查看网络即时网速 1.sar命令(一般般) sar -n DEV 1 100 1代表一秒统计并显示一次 100代表统计一百次 sar在sysstat包 2.使用ntop图形工具(没详细用过 ...
- iOS开发——实时监控网速(仅作参考,发现一点问题)
开发中用到获取网速的地方,应该就两种: 1.下载速度,这种可以直接在接受数据的地方统计计算.这个就不讲了. 2.获取手机网卡的数据,可以监控网卡的进出流量,下面就是. #import "Vi ...
- Nload(CentOS网速的实时监控)
Nload(CentOS网速的实时监控)的安装和安装过程中的问题 I. 安装 Download the latest rpmforge-release rpm from wget ftp://ftp. ...
- iOS 仿看了吗应用、指南针测网速等常用工具、自定义弹出视图框架、图片裁剪、内容扩展等源码
iOS精选源码 扩展内容的cell - folding-cell 一个近乎完整的可识别中国身份证信息的Demo 可自动快速... JPImageresizerView 仿微信的图片裁剪 带年月和至今以 ...
随机推荐
- TextView+Fragment实现底部导航栏
前言:项目第二版刚上线没多久,产品又对需求进行了大改动,以前用的是左滑菜单,现在又要换成底部导航栏,于是今天又苦逼加班了.花了几个小时实现了一个底部导航栏的demo,然后总结一下.写一篇博客.供自己以 ...
- 3、Redis 基础
Redis的五大数据类型 String(字符串) string是redis最基本的类型,你可以理解成与Memcached一模一样的类型,一个key对应一个value.string类型是二进制安全的.意 ...
- 如何用Excel直接查询Oracle中的数据
将Oracle中查询的数据保存为Excel文件,通常使用的是PL/SQL Developer. 其实,Excel可直接写SQL语句查询Oracle中数据,在这里,用到ODBC驱动.详细步骤如下: 一. ...
- canvas 图片拖拽旋转之二——canvas状态保存(save和restore)
引言 在上一篇日志“canvas 图片拖拽旋转之一”中,对坐标转换有了比较深入的了解,但是仅仅利用坐标转换实现的拖拽旋转,会改变canvas坐标系的状态,从而影响画布上其他元素的绘制.因此,这个时候需 ...
- 跟我学PHP-第一篇:如何安装Zend Studio 以及汉化和基本准备工作
昨天从早上一直弄到晚上10点,可累死我了,网上的资料都是掺次不齐,所以我写一篇系统点的文章来告诉大家怎么做. 1.如果你想进行一套PHP系统的开发,肯定是要有“尚方宝剑”的,这个尚方宝剑就是PHP工具 ...
- 4-MSP430定时器_定时器中断
一开始没写好就上传了,,,,,,,,这次来个全的 自己学MSP430是为了写一篇关于PID的文章,需要430在proteus上做仿真,一则认为在自动控制算法上PID真的很经典,PLC设备上大多是模块式 ...
- centos-5.5安装vmvare tools
centos-5.5安装vmvare tools 虚拟机管理,安装tools 找到VMwareTools压缩包 解压到Desktop,桌面 终端进入桌面 执行程序# ./vmware-install. ...
- redux+flux(一:入门篇)
React是facebook推出的js框架,React 本身只涉及UI层,如果搭建大型应用,必须搭配一个前端框架.也就是说,你至少要学两样东西,才能基本满足需要:React + 前端框架. Faceb ...
- 【Win10开发】自定义标题栏
UWP 现在已经可以自定义标题栏了,毕竟看灰色时间长了也会厌烦,开发者们还是希望能够将自己的UI做的更加漂亮,更加与众不同.那么废话不多说,我们开始吧! 首先要了解ApplicationViewTit ...
- 谈一谈SQL Server中的执行计划缓存(上)
简介 我们平时所写的SQL语句本质只是获取数据的逻辑,而不是获取数据的物理路径.当我们写的SQL语句传到SQL Server的时候,查询分析器会将语句依次进行解析(Parse).绑定(Bind).查询 ...