Click here to Skip to main content
16,020,567 members
Articles / Web Development / ASP.NET
Article

Bandwidth throttling

Rate me:
Please Sign up or sign in to vote.
4.75/5 (59 votes)
2 Apr 2007CPOL2 min read 285.3K   8.2K   139   61
Save bandwidth and get QoS with bandwidth throttling.

Screenshot - screen.jpg

Introduction

Hosting a website can be fun. But, when you offer big downloads or streaming media, you do not want a visitor to eat up the full bandwidth. Or, maybe, you want to offer a premium account from where users can download without limitation, and a free account where users cannot download faster than 50 kb/s. Here is where throttling comes in.

Bandwidth throttling helps provide quality of service (QoS) by limiting network congestion and server crashes. For example, you make sure a connection does not get more than an X number of bytes per second.

The purpose of this article is to show how to use bandwidth throttling, with a small helper class.

Using the code

Within the source code, you will find a class named ThrottledStream. This class is derived from the abstract Stream that can be found in the System.IO namespace. In the constructor, it accepts a base stream to throttle. Here is a small line of code that shows how to instantiate a ThrottledStream:

C#
Stream throttledStream = new ThrottledStream(Response.OutputStream, 1024);

Now, everything you do with the throttledStream will be limited to 1024 bytes per second, and will be send to or read from the OutputStream that is a member of the Response property that exists within an ASP.NET page.

Because the ThrottledStream is derived from the abstract Stream class, it is easy to add throttling to an existing application or website. For example, when you have a process that sends file content over a Stream and you want to enable bandwidth throttling, you only have to change the initialization of the destination stream.

The old code can look like this:

C#
Stream sourceStream;
Stream destinationStream;

try
{
    sourceStream = new FileStream(@"c:\myfile.bin", 
                       FileMode.Open, FileAccess.Read, FileShare.Read);
    destinationStream = new NetworkStream(mySocket, false);

    byte[] buffer = new byte[1024];
    int readCount = sourceStream.Read(buffer, 0, BufferSize);

    while (readCount > 0)
    {
        destinationStream.Write(buffer, 0, readCount);
        readCount = sourceStream.Read(buffer, 0, BufferSize);
    }
}
finally
{
    if (destinationStream != null)
    {
        destinationStream.Close();
    }
 
    if (sourceStream != null)
    {
        sourceStream.Close();
    }
}

Now, we can easily add throttling support to this process. We only need to change the initialization:

C#
...

Stream originalDestinationStream = new NetworkStream(mySocket, false);
destinationStream = new ThrottledStream(originalDestinationStream, 51200);

...

By adding only one line of code, this full process is throttled to 50 kb/s (51200 b/s). Now, we go even a step further and add throttling that is based on a membership:

C#
...

long bps;

switch( user.Membership )
{
    case MembershipLevel.Bronze:
        bps = 51200;
        break;

    case MembershipLevel.Silver:
        bps = 102400;
        break;

    case MembershipLevel.Gold:
        bps = 153600;
        break;

    case MembershipLevel.Platina:
        bps = ThrottledStream.Infinite;
        break;
}

Stream originalDestinationStream = new NetworkStream(mySocket, false);
destinationStream = new ThrottledStream(originalDestinationStream, bps);

...

Here, we have a situation where a Bronze membership will give you 50 kb/s, Silver 100 kb/s, Gold 150 kb/s, and Platina infinitive - no throttling.

Points of interest

Bandwidth throttling can improve the QoS of your server, and allows you to control the bandwidth for a specified connection. The helper class named ThrottledStream is very easy to use, and can be used in existing scenarios.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Sogyo
Netherlands Netherlands
Pieter Joost is a sr. IT consultant at Sogyo. He is recently awarded as Microsoft Visual C# MVP. He's active in the software development community as a speaker and boardmember of devnology.nl and dotned.nl

Comments and Discussions

 
GeneralRe: I'm sorry to say the solution is NOT correct Pin
P.J. van de Sande10-Apr-07 9:23
P.J. van de Sande10-Apr-07 9:23 
General5 stars code Pin
ntorrisi4-Apr-07 8:42
ntorrisi4-Apr-07 8:42 
GeneralRe: 5 stars code Pin
P.J. van de Sande4-Apr-07 20:55
P.J. van de Sande4-Apr-07 20:55 
QuestionClient Side? Pin
NickHD3-Apr-07 17:07
NickHD3-Apr-07 17:07 
AnswerRe: Client Side? Pin
P.J. van de Sande3-Apr-07 21:36
P.J. van de Sande3-Apr-07 21:36 
GeneralRe: Client Side? Pin
Bit-Smacker17-Aug-07 7:43
Bit-Smacker17-Aug-07 7:43 
Generalhelpful article Pin
Christiaan van Bergen2-Apr-07 21:25
professionalChristiaan van Bergen2-Apr-07 21:25 
Generaluseful Pin
lxwde2-Apr-07 19:33
lxwde2-Apr-07 19:33 
also want to see how you implement it in the article
GeneralRe: useful Pin
P.J. van de Sande3-Apr-07 0:36
P.J. van de Sande3-Apr-07 0:36 
GeneralInteresting, but.. Pin
Rafael Nicoletti2-Apr-07 11:03
Rafael Nicoletti2-Apr-07 11:03 
GeneralRe: Interesting, but.. Pin
Rei Miyasaka3-Apr-07 0:28
Rei Miyasaka3-Apr-07 0:28 
GeneralRe: Interesting, but.. Pin
P.J. van de Sande3-Apr-07 0:34
P.J. van de Sande3-Apr-07 0:34 
GeneralRe: Interesting, but.. Pin
Rei Miyasaka3-Apr-07 0:47
Rei Miyasaka3-Apr-07 0:47 
GeneralRe: Interesting, but.. Pin
P.J. van de Sande3-Apr-07 1:49
P.J. van de Sande3-Apr-07 1:49 
GeneralRe: Interesting, but.. Pin
Rei Miyasaka3-Apr-07 2:10
Rei Miyasaka3-Apr-07 2:10 
GeneralRe: Interesting, but.. Pin
YBS10-Apr-07 1:28
YBS10-Apr-07 1:28 
GeneralRe: Interesting, but.. Pin
P.J. van de Sande10-Apr-07 2:39
P.J. van de Sande10-Apr-07 2:39 
GeneralRe: Interesting, but.. Pin
Chris Maunder7-May-07 6:32
cofounderChris Maunder7-May-07 6:32 
GeneralRe: Interesting, but.. Pin
kishorevj7-Aug-07 0:53
kishorevj7-Aug-07 0:53 
GeneralRe: Interesting, but.. Pin
KraGiE9-Jul-09 0:39
KraGiE9-Jul-09 0:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.