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 仿微信的图片裁剪 带年月和至今以 ...
随机推荐
- EasyUI DataGrid 修改每页显示数量的最大值&&导出Grid到Excel
首先,最近在搞那个DataGrid的导出,发现,网上的用JS导出到本地的方法虽然可用,但是只能导出DataGrid当前的数据,例如说,DataGrid默认是最大显示50行,但是如果有多页,那么就无法显 ...
- Openfire/XMPP学习之——一个简单的Smack样例
昨天讲了Openfire的搭建和配置,今天来讲一下Smack.如果对如何搭建和配置Openfire的,可以参考Openfire/XMPP学习之——Openfire的安装.配置. Smack是一个开源, ...
- linux中断与异常
看了<深入理解linux内核>的中断与异常,简单总结了下,如果有错误,望指正! 一 什么是中断和异常 异常又叫同步中断,是当指令执行时由cpu控制单元产生的,之所以称之为异常,是因为只有在 ...
- 使用CSS3 制作一个material-design 风格登录界面
心血来潮,想学学 material design 的设计风格,就尝试完成了一个登录页面制作. 这是整体效果. 感觉还不错吧,结尾会附上代码 在编写的过程中,没有使用任何图片或者字体图标,全部使用css ...
- Java服务器对外提供接口以及Android端向服务器请求数据
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/5056780.html 讲解下java服务器是如何对移动终端提供接口的,以什么数据格式提供出去,移动端又是怎么 ...
- spring4+hibernate4+struts2项目整合的步骤及注意事项
首先,在整合框架之前,我们需要知道Spring框架在普通Java project和Web project中是略有不同的. 这个不同地方就在于创建IOC容器实例的方式不同,在普通java工程中,可以在m ...
- hibernate笔记--单(双)向的多对多映射关系
在讲单向的多对多的映射关系的案例时,我们假设我们有两张表,一张角色表Role,一张权限表Function,我们知道一个角色或者说一个用户,可能有多个操作权限,而一种操作权限同时被多个用户所拥有,假如我 ...
- DotNet的JSON序列化与反序列化
JSON(JavaScript Object Notation)JavaScript对象表示法,它是一种基于文本,独立于语言的轻量级数据交换格式.在现在的通信中,较多的采用JSON数据格式,JSON有 ...
- iOS多线程技术方案
iOS多线程技术方案 目录 一.多线程简介 1.多线程的由来 2.耗时操作的模拟试验 3.进程和线程 4.多线程的概念及原理 5.多线程的优缺点和一个Tip 6.主线程 7.技术方案 二.Pthrea ...
- 数据结构(C语言第2版)-----数组,广义表,树,图
任何一个算法的设计取决于选定的数据结构,而算法的实现依赖于采用的存储结构. 之前线性表的数据元素都是非结构的原子类型,元素的值是不可再分的.下面学习的这两个线性表是很特殊的,其中数据元素本身也可能是一 ...