views:

1489

answers:

1

I am developing a program that continually sends a stream of data in the background and I want to allow the user to set a cap for both upload and download limit.

I have read up on the token bucket and leaky bucket alghorhithms, and seemingly the latter seems to fit the description since this is not a matter of maximizing the network bandwidth but rather being as unobtrusive as possible.

I am however a bit unsure on how I would implement this. A natural approach is to extend the abstract Stream class to make it simple to extend existing traffic, but would this not require the involvement of extra threads to send the data while simultaneously receiving (leaky bucket)? Any hints on other implementations that do the same would be appreciated.

Also, although I can modify how much data the program receives, how well does bandwidth throttling work at the C# level? Will the computer still receive the data and simply save it, effectively canceling the throttling effect or will it wait until I ask to receive more?

EDIT: I am interested in throttling both incoming and outgoing data, where I have no control over the opposite end of the stream.

+9  A: 

See this article about ThrottledStream class. It should fit your needs.

arul