如何限制C中的套接字速度? [英] How do I limit socket speed in C?
问题描述
我正在用Unix编写一个简单的FTP服务器。作为服务器的一项功能,我想限制用户的上传/下载速度。
-
是否有任何库函数直接解决这个问题?
-
如果不是,生产FTP服务器中使用的算法是什么?我有一个非常天真的解决方案:计算一秒钟内发送多少个字节,比如
x
,write(x)
或read(x)
,然后sleep(1)
。
应该有更好的解决方案。甚至更好,如果有代码示例。
要清楚,我使用的是Mac OS X,但我希望它也可以在Ubuntu或某些Linux下运行。
p>FTP是一种应用层协议。 FTP可以在TCP或UDP套接字上运行。 (编辑:tftp和 uftp 在udp上运行,详情请参阅评论部分)
套接字速度是以下功能:
- 链接速度:10/100 Base T和等等。
链路的BER:误码率(通常在当今世界中,它通常是10到9的功率或类似的东西,也有突发错误。 / li>
- 套接字缓冲区大小:
/ proc / sys / net / core
参数
在linux上:为了处理TCP套接字,这里有一篇很好的文章[1]
4.调整堆栈以过滤/引入数据包丢失,从而最终限制流量:像 netem [2]这样的工具可以帮助您调整数据块以限制数据流。
[1] http://www.cyberciti.biz/faq/linux-tcp-tuning /
[2] http://www.linuxfoundation.org/协作/工作组/网络/ netem
Possible Duplicate:
How do you throttle the bandwidth of a socket connection in C?
I'm writing a simple FTP server in C for a Unix environment. As a feature of the server, I want to limit the upload/download speed of a user.
Are there any library functions directly solve this problem?
If not, what's the algorithm used in a production FTP server? I have a very naive solution: calculate how many bytes to send in a second, say
x
,write(x)
orread(x)
, and thensleep(1)
.
There should be a better solution. Even better if there are code samples.
To be clear, I'm using Mac OS X, but I wish it could also run under Ubuntu or some Linux.
FTP is a application layer protocol . FTP can run on TCP or UDP sockets. (EDIT: tftp and uftp runs on udp, pls see the comment section for details)
The Socket Speed is a function of following :
- link Speed :10/100 Base T and so on.
- BER of the link : Bit Error rate (typically in today's world its generally low 10 to the power -9 or something of that sort . there are burst errors as well.
- Socket Buffer sizes :
/proc/sys/net/core
parameters
On linux : For handling the TCP Sockets here's a good article [1]
4.Tweak the stack to filter/drop packets to introduce the packet loss , thus eventually throttling the flow rate : Tools like netem[2] help you adjust the buckets to throttle the flows.
[1] http://www.cyberciti.biz/faq/linux-tcp-tuning/
[2] http://www.linuxfoundation.org/collaborate/workgroups/networking/netem
这篇关于如何限制C中的套接字速度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!