Created
August 9, 2016 08:20
-
-
Save adow/e96f9272bd32ae0746d62d91643c692e to your computer and use it in GitHub Desktop.
获取网速
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// NetworkInterface.c | |
// wxlive | |
// | |
// Created by 秦 道平 on 16/8/3. | |
// Copyright © 2016年 秦 道平. All rights reserved. | |
// | |
#include "NetworkInterface.h" | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <ifaddrs.h> | |
#include <net/if.h> | |
#include <string.h> | |
long long get_interface_bytes() { | |
struct ifaddrs *ifa_list = 0, *ifa; | |
if (getifaddrs(&ifa_list) == -1) | |
{ | |
return 0; | |
} | |
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; | |
} | |
} | |
freeifaddrs(ifa_list); | |
return iBytes + oBytes; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment