如何获取当前带宽(下载)速度?

发布于 2024-12-23 03:15:43 字数 104 浏览 2 评论 0原文

如何使用 IdTCPServer 或 IdTCPClient 获取当前带宽速度?

我想知道,客户端从服务器下载数据的速度有多快?

例如:下载速度:450 kbps

How to get current bandwith speed using IdTCPServer or IdTCPClient ?

I want to know, how fast client is downloading data from server ?

e.g.: Downloading speed: 450 kbps

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔女人霸气范 2024-12-30 03:15:43

将处理程序分配给连接的 TIdTCPConnection 对象的 OnWorkBeginOnWorkOnWorkEnd 事件。 OnWorkBegin 事件有一个 AWorkCountMax 参数,该参数为您提供预计传输的总字节数(如果提前知道)。 OnWork 事件有一个 AWorkCount 参数,可提供自触发 OnWorkBegin 事件以来实际传输的字节总数。

每当触发 OnWork 事件时,从当前 AWorkCount 值中减去先前的 AWorkCount 值,以确定两个事件之间已传输了多少字节,然后将该值除以两个事件之间经过的时间量。利用该最终值,您可以根据需要计算 b/sec、kb/sec、mb/sec 等。

根据您发送/接收数据的具体方式,您可能需要手动调用 TIdTCPConnectionBeginWork()EndWork() 方法让 OnWork... 事件开始触发。大多数 Indy 的读/写方法不会在内部调用 Begin/EndWork()

Assign handlers to the OnWorkBegin, OnWork, and OnWorkEnd events of the connection's TIdTCPConnection object. The OnWorkBegin event has an AWorkCountMax parameter that gives you the total expected bytes being transfered (if known ahead of time). The OnWork event has an AWorkCount parameter that gives you a running total of how many bytes have actually been transferred since the OnWorkBegin event was fired.

Whenever the OnWork event is fired, subtract the previous AWorkCount value from the current AWorkCount value to determine how many bytes have been transferred between the two events, and then divide that value by the amount of time that has elapsed between the two events. With that final value, you can calculate b/sec, kb/sec, mb/sec, etc as needed.

Depending on how exactly you are sending/receiving your data, you may have to manually call the BeginWork() and EndWork() methods of TIdTCPConnection to get the OnWork... events to start firing. Most of Indy's read/write methods do not call Begin/EndWork() internally.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文