时时网速监控,方便全局监控当前流量使用

import

// 88kB/s

extern NSString *const GSDownloadNetworkSpeedNotificationKey;

// 2MB/s

extern NSString *const GSUploadNetworkSpeedNotificationKey;

@interface GSNetworkSpeed : NSObject

@property (nonatomic, copy, readonly) NSString*downloadNetworkSpeed;

@property (nonatomic, copy, readonly) NSString *uploadNetworkSpeed;

+ (instancetype)shareNetworkSpeed;

- (void)start;

- (void)stop;

@end

#import "GSNetworkSpeed.h"

#include

#include

#include

#include

NSString* const GSDownloadNetworkSpeedNotificationKey = @"GSDownloadNetworkSpeedNotificationKey";

NSString* const GSUploadNetworkSpeedNotificationKey = @"GSUploadNetworkSpeedNotificationKey";

@interface GSNetworkSpeed () {

//总网速

uint32_t _iBytes;

uint32_t _oBytes;

uint32_t _allFlow;

//wifi网速

uint32_t _wifiIBytes;

uint32_t _wifiOBytes;

uint32_t _wifiFlow;

//3G网速

uint32_t _wwanIBytes;

uint32_t _wwanOBytes;

uint32_t _wwanFlow;

}

@property (nonatomic, strong) NSTimer* timer;

@end

@implementation GSNetworkSpeed

static GSNetworkSpeed* instance = nil;

+ (instancetype)shareNetworkSpeed

{

static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{

instance = [[self alloc] init];

});

return instance;

}

+ (instancetype)allocWithZone:(struct _NSZone*)zone

{

static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{

instance = [super allocWithZone:zone];

});

return instance;

}

- (instancetype)init

{

if (self = [super init]) {

_iBytes = _oBytes = _allFlow = _wifiIBytes = _wifiOBytes = _wifiFlow = _wwanIBytes = _wwanOBytes = _wwanFlow = 0;

}

return self;

}

#pragma mark - 开始监听网速

- (void)start

{

if (_timer == nil) {

_timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(checkNetworkSpeed) userInfo:nil repeats:YES];

[_timer fire];

}

}

//停止监听网速

- (void)stop

{

if ([_timer isValid]) {

[_timer invalidate];

_timer = nil;

}

}

- (NSString*)stringWithbytes:(int)bytes

{

if (bytes < 1024) // B

{

return [NSString stringWithFormat:@"%dB", bytes];

}

else if (bytes >= 1024 && bytes < 1024 * 1024) // KB

{

return [NSString stringWithFormat:@"%.1fKB", (double)bytes / 1024];

}

else if (bytes >= 1024 * 1024 && bytes < 1024 * 1024 * 1024) // MB

{

return [NSString stringWithFormat:@"%.1fMB", (double)bytes / (1024 * 1024)];

}

else // GB

{

return [NSString stringWithFormat:@"%.1fGB", (double)bytes / (1024 * 1024 * 1024)];

}

}

- (void)checkNetworkSpeed

{

struct ifaddrs *ifa_list = 0, *ifa;

if (getifaddrs(&ifa_list) == -1) {

return;

}

uint32_t iBytes = 0;

uint32_t oBytes = 0;

uint32_t allFlow = 0;

uint32_t wifiIBytes = 0;

uint32_t wifiOBytes = 0;

uint32_t wifiFlow = 0;

uint32_t wwanIBytes = 0;

uint32_t wwanOBytes = 0;

uint32_t wwanFlow = 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;

// network

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;

allFlow = iBytes + oBytes;

}

//wifi

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 or gprs

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 != 0) {

_downloadNetworkSpeed = [[self stringWithbytes:iBytes - _iBytes] stringByAppendingString:@"/s"];

[[NSNotificationCenter defaultCenter] postNotificationName:GSDownloadNetworkSpeedNotificationKey object:nil];

NSLog(@"_downloadNetworkSpeed : %@",_downloadNetworkSpeed);

}

_iBytes = iBytes;

if (_oBytes != 0) {

_uploadNetworkSpeed = [[self stringWithbytes:oBytes - _oBytes] stringByAppendingString:@"/s"];

[[NSNotificationCenter defaultCenter] postNotificationName:GSUploadNetworkSpeedNotificationKey object:nil];

NSLog(@"_uploadNetworkSpeed :%@",_uploadNetworkSpeed);

}

_oBytes = oBytes;

}

@end

ios 网速监控_IOS_网速流量实时监控相关推荐

  1. 六、手把手教你搭建SpringCloudAlibaba之Sentinel实现流量实时监控

    SpringCloud Alibaba全集文章目录: 零.手把手教你搭建SpringCloudAlibaba项目 一.手把手教你搭建SpringCloud Alibaba之生产者与消费者 二.手把手教 ...

  2. 一款免费的网络流量实时监控软件

    一款免费的网络流量实时监控软件 来源版块: 网络安全 压缩包内文件格式: 可执行文件 附件来源: 互联网 运行平台: Windows平台 是否经本人验证: 是 附件性质: 免费 详细说明: ySoft ...

  3. Linux(CentOS)网络流量实时监控(iftop)

    在类Unix系统中可以使用 top 查看系统资源,进程,内存占用等信息.查看网络状态可以使用 netstat . nmap 等工具. 若要查看实时的网络流量,监控TCP/IP连接,反向解析IP,显示端 ...

  4. Linux网络流量实时监控ifstat iftop命令详解

    ifstat 介绍 ifstat工具是个网络接口监测工具,比较简单看网络流量 实例 默认使用 #ifstateth0 eth1 KB/s in KB/s out KB/s in KB/s out0.0 ...

  5. 冷链物流运转 3D 可视化监控,助力大数据实时监控

    智慧物流是以信息化为依托并广泛应用物联网.人工智能.大数据.云计算等技术工具,在物流价值链上的 6 项基本环节(运输.仓储.包装.装卸搬运.流通加工.配送)实现系统感知和数据采集的现代综合智能型物流系 ...

  6. 【无标题】电力监控系统如何实现用电实时监控-安科瑞 蒋静

    摘要:介绍上海东华大学电力监控系统,采用综合保护装置.多功能仪表.变压器温控仪.直流屏,采集配电现场的各种电参量和状态信号.系统采用现场就地组网的方式,组网后通过现场总线通讯并远传至后台,通过安科瑞电 ...

  7. Linux下网络流量实时监控

    工具1 sar sar命令包含在sysstat工具包中,提供系统的众多统计数据.其在不同的系统上命令有些差异,这里只说其在网络数据监控上的参数. sar -n [DEV|EDEV|SOCK] inte ...

  8. 是谁占用了我们的带宽:流量实时监控

    今天发现上网速度有点慢,估计是局域网内有人在下电影.于是,出于好奇就打算看看是谁在占用带宽.简单几个步骤看看谁是"罪魁祸首"  :) 1.网关:ssh 由于局域网是通过一台Linu ...

  9. 怎么监控mysql数据变化_实时监控mysql数据库变化

    对于二次开发来说,很大一部分就找找文件和找数据库的变化情况 对于数据库变化.还没有发现比较好用的监控数据库变化监控软件. 今天,我就给大家介绍一个如何使用mysql自带的功能监控数据库变化 1.打开数 ...

  10. 监控mysql数据库 更新_实时监控mysql数据库变化

    对于二次开发来说,很大一部分就找找文件和找数据库的变化情况 对于数据库变化.还没有发现比较好用的监控数据库变化监控软件. 今天,我就给大家介绍一个如何使用mysql自带的功能监控数据库变化 1.打开数 ...

最新文章

  1. 融资2.75亿美元的老牌AR公司,倒了
  2. 矩阵管理——本质是职能分工,例如所有部门都执行财务部门制定的财务制度而不会各自为政...
  3. Javascript执行上下文和执行栈
  4. Windows下安装Python模块时环境配置
  5. 前端学习(3289):object.define2
  6. 获取本机IP(考虑多块网卡、虚拟机等复杂情况)
  7. 细数那些年我用过的前端开发工具
  8. 创建RMAN CATALOG
  9. 在PyCharm中自动添加文件头、时间日期等信息
  10. HTTP-meta标签
  11. Java程序员集合框架面试题
  12. 微信小程序下载文件ios无法预览问题--解决方案
  13. HCIA—冲突域与广播域(详解 + 区别)
  14. 在SQL Server 2000里设置和使用数据库复制
  15. 逻辑思维训练和推理思维训练
  16. Excel:如何批量将文本数字转为数值型数据
  17. XBee zigbee 使用指南---XBee API模式示例
  18. Genymotion无法连接网络的解决办法
  19. 昆山市地方税务局异地复制及备份系统询价采购招标公告
  20. Beacon学习总结

热门文章

  1. 2019(第八届)国际桥梁与隧道技术大会-会议议程
  2. 论文阅读:Face-Mic: Inferring Live Speech and Speaker Identity via Subtle Facial Dynamics Captured by
  3. OSChina 周六乱弹 —— 正负能量交锋,谁是赢家?
  4. uniapp快速入门开发
  5. Zend与PHP之间到底是什么关系
  6. 盘点那些值得吐槽的计算机术语翻译
  7. UVA1336 Fixing the Great Wall 洛谷P2466 [SDOI2008]Sue的小球【区间DP记忆化搜索】
  8. Hola Studio最佳实践之多边形刚体的编辑
  9. MySQL的函数以及相关案例与练习
  10. Android开发这么久你竟然还不知道Dropbox?