//
// CCNetwork.h
// CCFC
//
// Created by xichen on 11-12-23.
// Copyright 2011 ccteam. All rights reserved.
//
#import <Foundation/Foundation.h>
#include <ifaddrs.h>
#include <sys/socket.h>
#include <net/if.h>
#define MACRO_LOCAL_LOOP_IP @"127.0.0.1"
@interface CCNetwork : NSObject
{
}
// get the total info of network flow, if returns FALSE, the returns upFlow and downFlow value can't be used.
+ (BOOL)getTotalNetworkFlow:(size_t *)upFlow withDownFlow:(size_t *)downFlow;
@end
//
// CCNetwork.m
// CCFC
//
// Created by xichen on 11-12-23.
// Copyright 2011 ccteam. All rights reserved.
//
#import "CCNetwork.h"
@implementation CCNetwork
// get the total info of network flow, if returns FALSE, the returns upFlow and downFlow value can't be used.
+ (BOOL)getTotalNetworkFlow:(size_t *)upFlow withDownFlow:(size_t *)downFlow
{
struct ifaddrs *ifa_list = 0, *ifa;
if (getifaddrs(&ifa_list) < 0)
{
return FALSE;
}
*upFlow = 0;
*downFlow = 0;
for (ifa = ifa_list; ifa; ifa = ifa->ifa_next)
{
if (ifa->ifa_addr->sa_family != AF_LINK)
continue;
if (!(ifa->ifa_flags & IFF_UP) && !(ifa->ifa_flags & IFF_RUNNING))
continue;
if (ifa->ifa_data == 0)
continue;
if (strncmp(ifa->ifa_name, "lo", 2))
{
struct if_data *if_data = (struct if_data *)ifa->ifa_data;
*downFlow += if_data->ifi_ibytes;
*upFlow += if_data->ifi_obytes;
}
}
freeifaddrs(ifa_list);
return TRUE;
}
@end
微风不燥,阳光正好,你就像风一样经过这里,愿你停留的片刻温暖舒心。
我是程序员小迷(致力于C、C++、Java、Kotlin、Android、Shell、JavaScript、TypeScript、Python等编程技术的技巧经验分享),若作品对您有帮助,请关注、分享、点赞、收藏、在看、喜欢,您的支持是我们为您提供帮助的最大动力。
欢迎关注。助您在编程路上越走越好!
标签:downFlow,upFlow,ios,list,ifa,data,CCNetwork From: https://blog.csdn.net/cxsjabcabc/article/details/7172523