你如何扼杀在C套接字连接的带宽? [英] How do you throttle the bandwidth of a socket connection in C?

查看:191
本文介绍了你如何扼杀在C套接字连接的带宽?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写使用BSD套接字客户端 - 服务器应用程序。它需要在后台运行,连续地传输数据,但不能养猪从正常使用的网络接口的带宽。取决于接口的速度,我需要节流该连接在一定的最大传输率。

I'm writing a client-server app using BSD sockets. It needs to run in the background, continuously transferring data, but cannot hog the bandwidth of the network interface from normal use. Depending on the speed of the interface, I need to throttle this connection to a certain max transfer rate.

什么是实现这一目标的最佳途径,编程?

What is the best way to achieve this, programmatically?

推荐答案

通过后每次传输,你将有波涛汹涌的网络性能睡1秒了一定量的问题。

The problem with sleeping a constant amount of 1 second after each transfer is that you will have choppy network performance.

让BandwidthMaxThreshold是所需的带宽阈值。

Let BandwidthMaxThreshold be the desired bandwidth threshold.

让TRANSFERRATE是连接的当前传输速率。

Let TransferRate be the current transfer rate of the connection.

然后...

如果您发现您的TRANSFERRATE> BandwidthMaxThreshold那么​​你做一个睡眠时间= 1 +休眠时间* 1.02(由2%提高睡眠时间)

If you detect your TransferRate > BandwidthMaxThreshold then you do a SleepTime = 1 + SleepTime * 1.02 (increase sleep time by 2%)

在或每个网络操作之后做
睡眠(睡眠时间)

Before or after each network operation do a Sleep(SleepTime)

如果您发现您的TRANSFERRATE比你BandwidthMaxThreshold你可以降低你的睡眠时间低了不少。或者你可以只衰减/减少你的睡眠时间随着时间的推移始终。最终,你的睡眠时间将再次达到0。

If you detect your TransferRate is a lot lower than your BandwidthMaxThreshold you can decrease your SleepTime. Alternatively you could just decay/decrease your SleepTime over time always. Eventually your SleepTime will reach 0 again.

而不是增加了2%,你也可以做一个增长量较大线性TRANSFERRATE之间的差异 - BandwidthMaxThreshold

Instead of an increase of 2% you could also do an increase by a larger amount linearly of the difference between TransferRate - BandwidthMaxThreshold.

此解决方案是好的,因为你没有睡觉,如果用户的网络已经没有那么高,你想。

This solution is good, because you will have no sleeps if the user's network is already not as high as you would like.

这篇关于你如何扼杀在C套接字连接的带宽?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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