关于实时监测网络每秒上下行流量问题
2017-12-09 09:44
246 查看
今天接到该需求,上午google了许多,cocoachina了一些,结合了前人的工作,整合了一些思路,封装了一个类,
用户监测当前每秒实时网络流量、wifi信号强弱度0-3、网络环境。现把代码发布。
.h文件
.m文件
用户监测当前每秒实时网络流量、wifi信号强弱度0-3、网络环境。现把代码发布。
.h文件
// // NetworkStateOrRSSI.h // GHZeusLibraries // // Created by 郭军 on 2017/12/2. // Copyright © 2017年 ZJBL. All rights reserved. // #import <Foundation/Foundation.h> typedef enum { GHNetworkErrorType = 0, GHNetwork2GType = 1, GHNetwork3GType = 2, GHNetwork4GType = 3, GHNetworkWifiType = 5 }GHNetworkType; @interface NSObject (NetworkStateOrRSSI) @property (assign, nonatomic) uint32_t nowIBytes; //当前每秒下行流量,KB @property (assign, nonatomic) uint32_t nowOBytes; //当前每秒上行流量 + (GHNetworkType)networkType; + (int)wifiStrengthBars; //开始检测,需每秒调用一次该方法,使获得nowIBytes&nowOBytes - (void)detectionBytes; @end
.m文件
// // NetworkStateOrRSSI.m // GHZeusLibraries // // Created by 郭军 on 2017/12/2. // Copyright © 2017年 ZJBL. All rights reserved. // #import "NSObject+NetworkStateOrRSSI.h" #include <ifaddrs.h> #include <sys/socket.h> #include <net/if.h> #import <UIKit/UIKit.h> #import <objc/runtime.h> @interface NSObject (bytes) @property (assign,nonatomic) uint32_t historyIBytes; @property (assign,nonatomic) uint32_t historyOBytes; @property (assign,nonatomic) uint32_t oldIBytes; @property (assign,nonatomic) uint32_t oldOBytes; @property (assign,nonatomic) BOOL isFirst; @end @implementation NSObject (bytes) - (void)setHistoryIBytes:(uint32_t)historyIBytes { objc_setAssociatedObject(self, @selector(historyIBytes), [NSNumber numberWithInt:historyIBytes], OBJC_ASSOCIATION_ASSIGN); } - (uint32_t)historyIBytes { return [objc_getAssociatedObject(self, _cmd) intValue]; } - (void)setHistoryOBytes:(uint32_t)historyOBytes { objc_setAssociatedObject(self, @selector(historyOBytes), [NSNumber numberWithInt:historyOBytes], OBJC_ASSOCIATION_ASSIGN); } - (uint32_t)historyOBytes { return [objc_getAssociatedObject(self, _cmd) intValue]; } - (void)setOldIBytes:(uint32_t)oldIBytes { objc_setAssociatedObject(self, @selector(oldIBytes), [NSNumber numberWithInt:oldIBytes], OBJC_ASSOCIATION_ASSIGN); } - (uint32_t)oldIBytes { return [objc_getAssociatedObject(self, _cmd) intValue]; } - (void)setOldOBytes:(uint32_t)oldOBytes { objc_setAssociatedObject(self, @selector(oldOBytes), [NSNumber numberWithInt:oldOBytes], OBJC_ASSOCIATION_ASSIGN); } - (uint32_t)oldOBytes { return [objc_getAssociatedObject(self, _cmd) intValue]; } - (void)setIsFirst:(BOOL)isFirst { objc_setAssociatedObject(self, @selector(isFirst), @(isFirst), OBJC_ASSOCIATION_COPY_NONATOMIC); } - (BOOL)isFirst { return [objc_getAssociatedObject(self, _cmd) boolValue]; } - (void)getInterfaceBytes { struct ifaddrs *ifa_list = 0, *ifa; if (getifaddrs(&ifa_list) == -1) { return; } uint32_t iBytes = 0; uint32_t oBytes = 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; /* Not a loopback device. */ 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; } } if (!self.isFirst) { self.historyIBytes = iBytes; self.historyOBytes = oBytes; self.isFirst=YES; } self.nowIBytes = (iBytes - self.historyIBytes)/1024 - self.oldIBytes; self.nowOBytes = (oBytes - self.historyOBytes)/1024 - self.oldIBytes; self.oldIBytes = (iBytes - self.historyIBytes)/1024; self.oldOBytes = (oBytes - self.historyOBytes)/1024; freeifaddrs(ifa_list); } @end @implementation NSObject (NetworkStateOrRSSI) - (void)setNowIBytes:(uint32_t)nowIBytes { objc_setAssociatedObject(self, @selector(nowIBytes), [NSNumber numberWithInt:nowIBytes], OBJC_ASSOCIATION_ASSIGN); } - (uint32_t)nowIBytes { return [objc_getAssociatedObject(self, _cmd) intValue]; } - (void)setNowOBytes:(uint32_t)nowOBytes { objc_setAssociatedObject(self, @selector(nowOBytes), [NSNumber numberWithInt:nowOBytes], OBJC_ASSOCIATION_ASSIGN); } - (uint32_t)nowOBytes { return [objc_getAssociatedObject(self, _cmd) intValue]; } + (GHNetworkType)networkType { UIApplication *app = [UIApplication sharedApplication]; NSArray *children = [[[app valueForKeyPath:@"statusBar"] valueForKeyPath:@"foregroundView"] subviews]; int type = 0; for (id child in children) { if ([child isKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")]) { type = [[child valueForKeyPath:@"dataNetworkType"] intValue]; } } return type; } + (int)wifiStrengthBars { UIApplication *app = [UIApplication sharedApplication]; NSArray *children = [[[app valueForKeyPath:@"statusBar"] valueForKeyPath:@"foregroundView"] subviews]; int type = 0; for (id child in children) { if ([child isKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")]) { type = [[child valueForKeyPath:@"wifiStrengthBars"] intValue]; } } return type; } - (void)detectionBytes { [self getInterfaceBytes]; } @end
相关文章推荐
- 关于Iris网络流量分析监测工具对本地TCP无法抓包的问题
- 关于网络流量的实时监测
- 安全运维之:网络实时流量监测工具iftop
- 关于IOS网络实时监听中reachability文件的问题
- 网络实时流量监测工具iftop
- 关于360安全卫士增加网络流量监控后上不了网的问题
- 网络实时流量监测工具iftop
- Linux网络实时流量监测工具iftop的安装使用
- 安全运维之:网络实时流量监测工具iftop
- 安全运维之:网络实时流量监测工具iftop
- 网络实时流量监测工具iftop
- 安全运维之:网络实时流量监测工具iftop
- 实时监测网络流量
- 最近,WannaCry勒索病毒肆虐全球,不少重要机构和企业的电脑纷纷中招,互联网上再次掀起关于网络安全的讨论。如今互联网技术发展飞速,随之而来的网络安全问题也越来越严重,其中网站遭遇流量攻击是比较突出
- 2010年汽车网络广告及网站季度流量监测专题
- 解决zabbix用snmp监控网络流量不准的问题 推荐
- iOS开发实时监测网络状态Reachability
- Linux网络流量实时监控ifstat iftop命令详解(转)
- iftop是一个很好用的实时流量监测程序,跟使用iftop查看linux连接进程占用的实时流量
- iOS 开发 AFNetworking实现网络状态的实时监测