How to limit socket speed in C?

Possible duplicate:
How do you throttle socket bandwidth in C?

I am writing a simple FTP server in C for a Unix environment. As a server feature, I want to limit the user upload / download speed.

  • Are there any library functions that directly solve this problem?

  • If not, what algorithm is used on the production FTP server? I have a very naive solution: calculate how many bytes to send per second, say x, write(x)or read(x), and then sleep(1).

There must be a better solution. Even better if there are code examples.

To be clear, I use Mac OS X, but I would like it to work under Ubuntu or Linux as well.

+5
source share
2 answers

Do you really want to do this? Does your motive annoy your users? (this is a legitimate motive - see any of several free download sites)

Limiting bandwidth like this is not a good way to protect your server from congestion. people will find multithreaded clients and open concurrent FTP sessions ...

Are there any library functions for this?

Perhaps not, bandwidth shaping is an OS task, not a service task.

which algorithm?

The one you describe sounds pretty effective.

, , , . , : , sleep() . .. .

usleep nanosleep posix, OSX * BSD linux.

0

FTP - . FTP TCP UDP. (EDIT: tftp uftp udp, PLS . )

Socket :

  • link : 10/100 Base T ..
  • BER : . ( 10 -9 - . .
  • Socket: /proc/sys/net/core

linux: TCP- [1]

4.Tweak , / , , : , netem [2], .

[1] http://www.cyberciti.biz/faq/linux-tcp-tuning/

[2] http://www.linuxfoundation.org/collaborate/workgroups/networking/netem

0

All Articles