android TrafficStats getUidRxBytes不正确 [英] android TrafficStats getUidRxBytes inaccurate

查看:119
本文介绍了android TrafficStats getUidRxBytes不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个小小的android应用程序,发送Http请求,接收来自服务器的响应,并计算了发送和接收的字节数. 代码如下所示

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

例如,我发现ReceivedBytes始终大于http Header + http Body的大小. 我在服务器中捕获(使用wireshark)的实际http帧大小为1645字节(header + body),但android API返回的receivedBytes为1912,因此是传输.

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.

TrafficStats的getUidRxBytes本身不准确(可能是该问题特定于我的带有cynogenmod 10.3的平台i9300)

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

最后,我找到了计算数据使用量的正确方法,也找到了其他方法来计算数据使用量,这似乎比TrafficStats API更准确.(非常感谢

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;
}

推荐答案

我看到您已经找到了解决方案,但是我会对您的问题发表自己的看法,因为这可能对其他人有用(在此处结束)自己搜寻了如何使用TrafficStats API之后.

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).

API文档指出:

统计信息是在网络层进行测量的,因此它们同时包括TCP和UDP使用情况.

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是应用层协议.在计算期望的字节数为HTTP标头字节加上HTTP正文字节时,您只处理应用程序层字节,因此不考虑传输和网络层标头字节.我假设使用TCP进行下载. 这会添加20到60个字节的标头.此外,假设您正在使用IPv4进行下载. 这还会添加一个20到60个字节的标头.

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.

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

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

有点题外话,但仍然相关.尚不清楚TrafficStats API是否实际计算标头字节.根据此答案,该API 对报头字节进行计数.但是,鉴于上面列出的API文档,链接的答案可能规定了不正确的内容(至少不是针对API级别21的情况).此外,此问题还暗示TrafficStats实际上正在计算网络和传输层报头字节(检查注释).

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实际上计算网络和传输层标头字节.参见内核源 TrafficStatsTest .

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

这篇关于android TrafficStats getUidRxBytes不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆