如何获取当前带宽(下载)速度?
如何使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将处理程序分配给连接的
TIdTCPConnection
对象的OnWorkBegin
、OnWork
和OnWorkEnd
事件。OnWorkBegin
事件有一个AWorkCountMax
参数,该参数为您提供预计传输的总字节数(如果提前知道)。OnWork
事件有一个AWorkCount
参数,可提供自触发OnWorkBegin
事件以来实际传输的字节总数。每当触发
OnWork
事件时,从当前AWorkCount
值中减去先前的AWorkCount
值,以确定两个事件之间已传输了多少字节,然后将该值除以两个事件之间经过的时间量。利用该最终值,您可以根据需要计算 b/sec、kb/sec、mb/sec 等。根据您发送/接收数据的具体方式,您可能需要手动调用
TIdTCPConnection
的BeginWork()
和EndWork()
方法让OnWork...
事件开始触发。大多数 Indy 的读/写方法不会在内部调用Begin/EndWork()
。Assign handlers to the
OnWorkBegin
,OnWork
, andOnWorkEnd
events of the connection'sTIdTCPConnection
object. TheOnWorkBegin
event has anAWorkCountMax
parameter that gives you the total expected bytes being transfered (if known ahead of time). TheOnWork
event has anAWorkCount
parameter that gives you a running total of how many bytes have actually been transferred since theOnWorkBegin
event was fired.Whenever the
OnWork
event is fired, subtract the previousAWorkCount
value from the currentAWorkCount
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()
andEndWork()
methods ofTIdTCPConnection
to get theOnWork...
events to start firing. Most of Indy's read/write methods do not callBegin/EndWork()
internally.