如何获得在C#.NET准确的下载/上传速度? [英] How to get accurate download/upload speed in C#.NET?

查看:608
本文介绍了如何获得在C#.NET准确的下载/上传速度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过一个网络接口使用C#.NET来得到准确的下载/上传速度 我知道,它可用下列公式计算 GetIPv4Statistics()。与BytesReceived 键,把线程睡眠一段时间。但它不给我我得到我的浏览器的输出。

I want to get accurate download/upload speed through a Network Interface using C# .NET I know that it can be calculated using GetIPv4Statistics().BytesReceived and putting the Thread to sleep for sometime. But it's not giving the output what I am getting in my browser.

推荐答案

通过查看另一个答案给你贴在<一个问题href="http://stackoverflow.com/questions/13579530/networkinterface-getipv4statistics-bytesreceived-what-does-it-return">NetworkInterface.GetIPv4Statistics().BytesReceived - 这是什么返回我相信这个问题可能是你使用的是小的间隔?我相信,计数器只计算全包,比如你正在下载的文件包可能会大如 64 KB 65,535字节,IPv4的最大封装尺寸),这是相当多的,如果你的最大下载吞吐量 60 KB / S 和你测量 200毫秒的间隔。

By looking at another answer to a question you posted in NetworkInterface.GetIPv4Statistics().BytesReceived - What does it return? I believe the issue might be that you are using to small intervals. I believe the counter only counts whole packages, and if you for example are downloading a file the packages might get as big as 64 KB (65,535 bytes, IPv4 max package size) which is quite a lot if your maximum download throughput is 60 KB/s and you are measuring 200 ms intervals.

由于你的速度是 60 KB / S 我会的运行时间设置为10秒,让每平均至少9包。如果你正在写它用于所有类型的连接,我会建议你做的解决方案动态的,也就是说,如果速度快,你可以​​很容易地降低平均间隔,但在低速连接的情况下,你必须增加平均间隔。

Given that your speed is 60 KB/s I would have set the running time to 10 seconds to get at least 9 packages per average. If you are writing it for all kinds of connections I would recommend you make the solution dynamic, ie if the speed is high you can easily decrease the averaging interval but in the case of slow connections you must increase the averaging interval.

要么是@pst建议通过具有移动平均线,或简单地增加睡眠可达也许1秒。

Either do as @pst recommends by having a moving average or simply increase the sleep up to maybe 1 second.

和一定要通过采取,而不是传递给的Thread.Sleep时的实际时间来划分()

And be sure to divide by the actual time taken rather than the time passed to Thread.Sleep().

在间隔其他想法

我的过程是这样的事情,测量5秒钟,并收集数据,即字节:收到,以及数据包的数量。

My process would be something like this, measure for 5 second and gather data, ie bytes recieved as well as the number of packets.

var timePerPacket = 5000 / nrOfPackets; // Time per package in ms
var intervalTime = Math.Max(d, Math.Pow(2,(Math.Log10(timePerPacket)))*100);

这将导致的时间间隔慢慢从增加约几十毫秒到每个分组的时间。这样一来,我们始终得到至少(平均)每间隔一包,我们不会发疯,如果我们在10 Gbps的连接。最重要的部分是,在测量时间不应该是线性的,以接收的数据量。

This will cause the interval to increase slowly from about several tens of ms up to the time per packet. That way we always get at least (on average) one package per interval and we will not go nuts if we are on a 10 Gbps connection. The important part is that the measuring time should not be linear to the amount of data received.

这篇关于如何获得在C#.NET准确的下载/上传速度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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