应用级别实现流量监控

类似那种三方软件,像netlimiter那种软件是如何可以监控到各个三方软件,将上传和下载速度检测出来?可以通过一些Api接口函数实现吗?或者有什么好的思路?监控的原理

linux下/proc/net目录下是网络流量实时数据也包括和pid关联,/proc//fd和/proc/net/tcp关联,就能又获取流量,有知道是哪些应用,楼主可以研究下,就不细讲了

您好,可参考如下
主要用到GetIfTable(),和GetIfEntry() 两个API。界面用QT做,可以隐藏在系统托盘中。


void NetFlow::getFlow() {  
    // 记录上一次函数调用时的总流量和时间,在后面计算流量速度的时候用到  
    preInFlow = inFlow;  
    preOutFlow = outFlow;  
    preTime = currTime;  
    inFlow = 0.0f;  
    outFlow = 0.0f;  
    currTime = GetTickCount();  
    int i;  
    pIfTable = (MIB_IFTABLE *) MALLOC(sizeof (MIB_IFTABLE));  
    if (pIfTable == NULL) {  
        printf("Error allocating memory needed to call GetIfTable/n");  
        return;  
    }  
    dwSize = sizeof (MIB_IFTABLE);  
    if (GetIfTable(pIfTable, &dwSize, 0) == ERROR_INSUFFICIENT_BUFFER) {  
        FREE(pIfTable);  
        pIfTable = (MIB_IFTABLE *) MALLOC(dwSize);  
        if (pIfTable == NULL) {  
            printf("Error allocating memory/n");  
            return;  
        }  
    }  
    if ((dwRetVal = GetIfTable(pIfTable, &dwSize, 0)) == NO_ERROR) {  
        if (pIfTable->dwNumEntries > 0) {  
            pIfRow = (MIB_IFROW *) MALLOC(sizeof (MIB_IFROW));  
            if (pIfRow == NULL) {  
                printf("Error allocating memory/n");  
                if (pIfTable != NULL) {  
                    FREE(pIfTable);  
                    pIfTable = NULL;  
                }  
                return;  
            }  
            printf("/tNum Entries: %ld/n/n", pIfTable->dwNumEntries);  
            // 枚举所有接口  
            for (i = 0; i < (int) pIfTable->dwNumEntries; i++) {  
                pIfRow->dwIndex = pIfTable->table[i].dwIndex;  
                if ((dwRetVal = GetIfEntry(pIfRow)) == NO_ERROR) {  
                    // 排除环回接口的流量  
                    if(pIfRow->dwType != MIB_IF_TYPE_LOOPBACK) {  
                        inFlow += pIfRow->dwInOctets;  
                        outFlow += pIfRow->dwOutOctets;                    
                    }  
                }  
                else {  
                    printf("GetIfEntry failed for index %d with error: %ld/n",  
                           i, dwRetVal);  
                }  
            }  
        } else {  
            printf("/tGetIfTable failed with error: %ld/n", dwRetVal);  
        }  
    }  
    FREE(pIfTable);  
    FREE(pIfRow);  
    // 计算流量,速度=总流量 / 所经过的时间  
    float inSpeed = (inFlow - preInFlow) / ((float)(currTime - preTime) / 1000.0f);  
    float outSpeed = (outFlow - preOutFlow) / ((float)(currTime - preTime) / 1000.0f);  
    char buffer[100];  
    sprintf(buffer, "Download: %.2fk/s Upload: %.2fk/s", inSpeed / 1000.0f, outSpeed / 1000.0f);  
    bodyEdit->setText(buffer);  
    trayIcon->setToolTip(QString(buffer));  
}   ```

void NetFlow::getFlow() {
// 记录上一次函数调用时的总流量和时间,在后面计算流量速度的时候用到
preInFlow = inFlow;
preOutFlow = outFlow;
preTime = currTime;
inFlow = 0.0f;
outFlow = 0.0f;
currTime = GetTickCount();
int i;
pIfTable = (MIB_IFTABLE *) MALLOC(sizeof (MIB_IFTABLE));
if (pIfTable == NULL) {
printf("Error allocating memory needed to call GetIfTable/n");
return;
}
dwSize = sizeof (MIB_IFTABLE);
if (GetIfTable(pIfTable, &dwSize, 0) == ERROR_INSUFFICIENT_BUFFER) {
FREE(pIfTable);
pIfTable = (MIB_IFTABLE *) MALLOC(dwSize);
if (pIfTable == NULL) {
printf("Error allocating memory/n");
return;
}
}
if ((dwRetVal = GetIfTable(pIfTable, &dwSize, 0)) == NO_ERROR) {
if (pIfTable->dwNumEntries > 0) {
pIfRow = (MIB_IFROW *) MALLOC(sizeof (MIB_IFROW));
if (pIfRow == NULL) {
printf("Error allocating memory/n");
if (pIfTable != NULL) {
FREE(pIfTable);
pIfTable = NULL;
}
return;
}
printf("/tNum Entries: %ld/n/n", pIfTable->dwNumEntries);
// 枚举所有接口
for (i = 0; i < (int) pIfTable->dwNumEntries; i++) {
pIfRow->dwIndex = pIfTable->table[i].dwIndex;
if ((dwRetVal = GetIfEntry(pIfRow)) == NO_ERROR) {
// 排除环回接口的流量
if(pIfRow->dwType != MIB_IF_TYPE_LOOPBACK) {
inFlow += pIfRow->dwInOctets;
outFlow += pIfRow->dwOutOctets;
}
}
else {
printf("GetIfEntry failed for index %d with error: %ld/n",
i, dwRetVal);
}
}
} else {
printf("/tGetIfTable failed with error: %ld/n", dwRetVal);
}
}
FREE(pIfTable);
FREE(pIfRow);
// 计算流量,速度=总流量 / 所经过的时间
float inSpeed = (inFlow - preInFlow) / ((float)(currTime - preTime) / 1000.0f);
float outSpeed = (outFlow - preOutFlow) / ((float)(currTime - preTime) / 1000.0f);
char buffer[100];
sprintf(buffer, "Download: %.2fk/s Upload: %.2fk/s", inSpeed / 1000.0f, outSpeed / 1000.0f);
bodyEdit->setText(buffer);
trayIcon->setToolTip(QString(buffer));
}

```

下载地址:http://download.csdn.net/source/3040966

github上边的程序,可以参考一下

inux 系统下所有的信息都是以文件的形式存在的。
应用程序的流量信息也被保存在操作系统的某一个文件中。
Android 2.2 版本以前的系统的流量信息都存放在
proc/net/dev
或者 proc/self/net/dev
目录下,读取文件然后对其进行解析就行了。
之后版本存放路径
proc/uid_stat/uid /tcp_rcv
proc/uid_stat/uid /tcp_snd
读取某个应用(uid)的流量,对以上两个文件进行解析即可(注:模拟器下不存在这个目录)。
查看某个应用的流量信息,可以通过以下命令来实现:

命令查看流量存储文件
查看uid等于1000的程序,流量使用数据

uid为1000的程序接受与发送的流量数据(B)
这里需要注意的是不同语言编写的应用程序对应的的 uid 命名规则是不同的:
Java 编写的应用程序,它的 uid 是从 10000 开始的;
C 语言编写的,它的 uid 是从 1000 开始的;
另外,uid 一旦被分配,是不会改变的。
从下图中我们可以看到类似u0_a0、u0_a1、u0_a3、u0_a4的信息,这些信息也代表了 uid:
u0_a0 等同于 10000
u0_a1 等同于 10001
事实上“u0_aXX”后面的信息代表这个应用的安装序号,表示这个应用是第几个被安装到系统的。
查看 /data/data 需要root权限

打印程序信息
程序包名与程序uid对应关系,存储在系统/data/system/packages.list文件中
需要root权限

查看uid列表
Android 2.2 版本开始加入了android.net.TrafficStats类来实现对流量统计的操作。
其实 TrafficStats 类也是读取 Linux 下保存流量信息的文件并进行解析来实现的。
类中提供了多种静态方法,可以直接调用,返回类型均为 long 型,如果返回值等于 -1 代表UNSUPPORTED,表示当前设备不支持统计。
这里需要注意的是,通过 TrafficStats 获取的数据在手机重启的时候会被清空,所以,如果要对流量进行持续的统计需要将数据保存到数据库中,在手机重启时将数据读出进行累加即可。
验证手机重启
重启手机后,查看流量相关文件,

uid_stat目录为空
说明数据已被清空了。
TrafficStats 类的静态方法如下:
static long  getMobileRxBytes()  //获取通过Mobile连接收到的字节总数,不包含WiFi
static long  getMobileRxPackets()  //获取Mobile连接收到的数据包总数
static long  getMobileTxBytes()  //Mobile发送的总字节数
static long  getMobileTxPackets()  //Mobile发送的总数据包数
static long  getTotalRxBytes()  //获取总的接受字节数,包含Mobile和WiFi等
static long  getTotalRxPackets()  //总的接受数据包数,包含Mobile和WiFi等
static long  getTotalTxBytes()  //总的发送字节数,包含Mobile和WiFi等
static long  getTotalTxPackets()  //发送的总数据包数,包含Mobile和WiFi等
static long  getUidRxBytes(int uid)  //获取某个程序的接受字节数
static long  getUidTxBytes(int uid) //获取某个程序的发送字节数
总接受流量TrafficStats.getTotalRxBytes();
总发送流量TrafficStats.getTotalTxBytes());
不包含WIFI的手机GPRS接收量TrafficStats.getMobileRxBytes());
不包含Wifi的手机GPRS发送量TrafficStats.getMobileTxBytes());
/** 获取手机指定 UID 对应的应程序用通过所有网络方式接收的字节流量总数(包括 wifi) /
某一个程序的总接收量TrafficStats.getUidRxBytes(Uid));
/
* 获取手机指定 UID 对应的应用程序通过所有网络方式发送的字节流量总数(包括 wifi) */
某一个程序的总发送量TrafficStats.getUidTxBytes(Uid));
从本次开机到本次调用该接口的统计量