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. EF7 - What Does “Code First Only” Really Mean

    这篇文章很有价值,但翻译了一段,实在翻译不下去了,没办法,只能转载了. 英文地址:http://blogs.msdn.com/b/adonet/archive/2014/10/21/ef7-what- ...

  2. [ES] 安装

    1.ElasticSearch安装的准备工作 Linux:CentOS6.4 Elasticsearc:elasticsearch-2.2.0 JDK:jdk-7u79-linux-x64 IK:1. ...

  3. Markdown 语法整理

    Markdown 语法整理 白宁超 2015年7月24日14:57:49 一.字体设置 A First Level Header == A Second Level Header -- # 标题 ## ...

  4. Anliven - 一碗毒鸡汤

    什么是你的核心动力,支撑着你持续前进? 什么是你的加速度,激发你全部的潜能和勇气? 你的核心动力应该来自于: 家人与朋友的信任.包容与期待 你本应承担的责任 对自己有所要求,有所期待,你本应更好 而你 ...

  5. DotNet中几种常用的加密算法

    在.NET项目中,我们较多的使用到加密这个操作.因为在现代的项目中,对信息安全的要求越来越高,那么多信息的加密就变得至关重要.现在提供几种常用的加密/解密算法. 1.用于文本和Base64编码文本的互 ...

  6. Lucene教程

    一:简单的示例 1.1:生成索引 1.1.1:Field.Store和Field.Index 1.1.2:为数字生成索引 1.1.3:为索引加权 1.1.4:为日期生成索引 1.2:查询 1.2.1: ...

  7. ros::spin() 和 ros::spinOnce() 区别及详解

    版权声明:本文为博主原创文章,转载请标明出处: http://www.cnblogs.com/liu-fa/p/5925381.html 博主提示:本文基于ROS Kinetic Kame,如有更(g ...

  8. 9.Configure One-to-One(配置一对一关系)【Code-First系列】

    现在,开始学习怎么配置一对一的关系,众所周知,一对一的关系是:一个表中的主键,在另外一个表中,同时是主键和外键[实际上是一对零或者一对一]. 请注意:一对一的关系,在MS SQL Server中,技术 ...

  9. 【原创】Kafka topic常见命令解析

    本文着重介绍几个常用的topic命令行命令,包括listTopic,createTopic,deleteTopic和describeTopic等.由于alterTopic并不是很常用,本文中就不涉及了 ...

  10. linq的简单增删改查

    Linq高集成化的数据访问类,它会自动映射数据库结构,将表名完整映射成为类名,将列名完整映射成字段名数据库数据访问,能大大减少代码量.(反正最后结果就是不用写ado.Net那一套增删改查,有一套封装好 ...