利用网络带宽C#-calculate下载/上传时间 [英] C# -calculate download/upload time using network bandwidth
本文介绍了利用网络带宽C#-calculate下载/上传时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
由于在林发展,我需要显示的总估计时间留给上传/文件从服务器下载到/应用程序的功能。
将如何。有可能得到的下载/上传速度从客户机服务器
我想,如果IM能够获得的速度,然后我可以计算出时间 - >
例如---一个200 MB的文件= 200(1024 KB)= 204800 KB和
将其划分204800兆/速度KB /秒=X秒
块引用>
解决方案尝试是这样的:
INT CHUNKSIZE = 1024;
INT发送= 0
INT总= reader.Length;
日期时间开始= DateTime.Now;
,而(reader.Position< reader.Length)
{
字节[]缓冲区=新的字节[
Math.Min(CHUNKSIZE,reader.Length - reader.Position) ];
的ReadBytes = reader.Read(缓冲液,0,buffer.Length);
//发送数据包
发送+ =的ReadBytes;
时间跨度elapsedTime = DateTime.Now - 启动;
时间跨度estimatedTime =
TimeSpan.FromSeconds(
(总 - 发送)/
((双)发送/ elapsedTime.TotalSeconds));
}
As a feature in the application which Im developing, I need to show the total estimated time left to upload/download a file to/from server.
how would it possible to get the download/upload speed to the server from client machine.
i think if im able to get speed then i can calculate time by -->
for example ---for a 200 Mb file = 200(1024 kb) = 204800 kb and divide it by 204800 Mb / speed Kb/s = "x" seconds
解决方案Try something like this:
int chunkSize = 1024; int sent = 0 int total = reader.Length; DateTime started = DateTime.Now; while (reader.Position < reader.Length) { byte[] buffer = new byte[ Math.Min(chunkSize, reader.Length - reader.Position)]; readBytes = reader.Read(buffer, 0, buffer.Length); // send data packet sent += readBytes; TimeSpan elapsedTime = DateTime.Now - started; TimeSpan estimatedTime = TimeSpan.FromSeconds( (total - sent) / ((double)sent / elapsedTime.TotalSeconds)); }
这篇关于利用网络带宽C#-calculate下载/上传时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文