android流量统计getUidRxBytes inaccurate[英] android TrafficStats getUidRxBytes inaccurate

本文是小编为大家收集整理的关于android流量统计getUidRxBytes inaccurate的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我编写一个小的Android应用程序,发送HTTP请求,从服务器接收响应,并计算多少个字节传输和接收. 代码仅如下

long receivedBytes = TrafficStats.getUidRxBytes(uid)-lastNumer

我发现接收者总是更大的尺寸HTTP标头+HTTP主体 我在服务器中捕获的实际HTTP框架的大小(使用Wireshark)为1645个字节(标头+车身),但是Android API返回的Bydyby是1912年,因此作为传输.

clogilstats getuidrxbytes本身不准确(可能是我的平台samsung i9300,cynogenmod 10.3)

)

最后,我找到了计算数据使用量的正确方法,我找到了其他方法来计数数据使用情况,这似乎比clofalsTats API更准确./traffilstats" rel =" nofollow">在这里)

private long[] getStat() {
    String line, line2;
    long[] stats = new long[2];
    try {
        File fileSnd = new File("/proc/uid_stat/"+uid+"/tcp_snd");
        File fileRcv = new File ("/proc/uid_stat/"+uid+"/tcp_rcv");
        BufferedReader br1 = new BufferedReader(new FileReader(fileSnd));
        BufferedReader br2 = new BufferedReader(new FileReader(fileRcv));
        while ((line = br1.readLine()) != null&& (line2 = br2.readLine()) != null) {
            stats[0] = Long.parseLong(line);
            stats[1] = Long.parseLong(line2);
        }
        br1.close();
        br2.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return stats;
}

推荐答案

我看到您已经找到了一个解决方案,但是我会在您的问题上添加我的想法,因为它对其他人可能很有用(在搜索搜索如何使用the Cloffalstats API之后,我自己最终在这里).

api docudment "> api docudment /p>

在网络层上测量统计信息,因此它们包括TCP和UDP使用.

文档确实可以更彻底,但是我倾向于说,可以假设返回的字节计数还包括组成传输层标题和网络层标题的字节.

http是一个应用程序层协议.当您计算预期的字节为HTTP标头字节以及HTTP主体字节时,您只处理应用程序层字节,因此不考虑传输和网络层标题字节.我认为TCP用于下载. 这增加了一个范围为20至60个字节.此外,假设您正在使用IPv4进行下载. 也增加了一个从20到60字节的标题..

.

显然,这不会说明整个1912 - 1645 = 267字节,但它可能会给您/其他人.


有点偏离主题,但仍然相关.目前尚不清楚交通端API是否真的计算标题字节.根据这个答案,API do not count count theake header字节.但是,鉴于上面列出的API文档,链接的答案可能是规定不正确的内容(至少不适合API级别21).此外,这个问题也暗示了claffilsTats实际上计数网络和传输层字节(检查评论).

.


交通状态实际上计数网络和传输层标题字节.参见

其他推荐答案

根据我的理解,您应该将getuidrxbytes与getuidrxpackets结合在一起.

您应该有类似的东西:getuidrxbytes = getUidrxpackets *(tcp/ip标头大小)

本文地址:https://www.itbaoku.cn/post/2091135.html

问题描述

I write a little android app, sends Http request, receives response from server, and count how many bytes transmitted and received. and the code is simply as follow

long receivedBytes = TrafficStats.getUidRxBytes(uid)-lastNumer

i find that the receivedBytes is always larger the size http Header+http Body, for example the actual http frame's size i caught( use wireshark) in server is 1645 bytes(header+body), but the android API returns receivedBytes is 1912, so as the transmission.

the TrafficStats getUidRxBytes itself is inaccurate(may be this problem is specific to my platform samsung i9300 with cynogenmod 10.3)

finally, i find the correct way to count the data usage i find other way to count the data usage which seems more accurate than TrafficStats API.(many thanks to here)

private long[] getStat() {
    String line, line2;
    long[] stats = new long[2];
    try {
        File fileSnd = new File("/proc/uid_stat/"+uid+"/tcp_snd");
        File fileRcv = new File ("/proc/uid_stat/"+uid+"/tcp_rcv");
        BufferedReader br1 = new BufferedReader(new FileReader(fileSnd));
        BufferedReader br2 = new BufferedReader(new FileReader(fileRcv));
        while ((line = br1.readLine()) != null&& (line2 = br2.readLine()) != null) {
            stats[0] = Long.parseLong(line);
            stats[1] = Long.parseLong(line2);
        }
        br1.close();
        br2.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return stats;
}

推荐答案

I see that you've already found a solution, but I'll add my thoughts on your question as it might be useful to other people (ended up here myself after googling how to use the TrafficStats API).

The API documentation states:

Statistics are measured at the network layer, so they include both TCP and UDP usage.

The documentation could indeed be more thorough, but I'm inclined to say that one can assume that the returned byte count also includes the bytes making up the transport layer header and the network layer header.

HTTP is an application layer protocol. When you're calculating your expected bytes to be the HTTP header bytes plus the HTTP body bytes, you're only dealing with application layer bytes, hence not accounting for transport and network layer header bytes. I assume TCP is used for the download. This adds a header ranging from 20 to 60 bytes. Moreover, let's assume you're using IPv4 for the download. This also adds a header ranging from 20 to 60 bytes.

Obviously this won't account for the entire 1912 - 1645 = 267 bytes, but it might give you/other people some leads.


A bit off-topic, but still related. It's not quite clear if the TrafficStats API actually count header bytes or not. According to this answer, the API does not count header bytes. However, given the API documentation listed above, the linked answer may be stipulating something that is not true (at least not for API level 21). Moreover, this question also hints at TrafficStats actually counting network and transport layer header bytes (check comments).


TrafficStats actually counts network and transport layer header bytes. See kernel source and TrafficStatsTest.

其他推荐答案

From my understanding, you should combine getUidRxBytes with getUidRxPackets.

You should have something like : getUidRxBytes = getUidRxPackets * (tcp/ip header size)