Link to home
Start Free TrialLog in
Avatar of stumpy1
stumpy1

asked on

Howto do bandwidth Throttling using C# .Net code

Hi,
I am building a windows forms application using C# that will copy files to remote servers etc.  I want to implement bandwidth throttling from within my application and cant seem to find any examples.
Can you give me a good example that shows the .Net API's and Classes that are needed to implement throttling through my application.

I only have this posted as a 50 pointer because I have a feeling that Im going to be told that it cant be done using C# .Net and that I need to use a 3rd party app.

However, If I am getting a C# .Net solution I will up the points
Avatar of gbzhhu
gbzhhu
Flag of United Kingdom of Great Britain and Northern Ireland image

what is bandwidth throttling?
Avatar of stumpy1
stumpy1

ASKER

You have a network pipe from 1 office to another, when an application is doing a copy or something over that pipe you dont want it using up all the available bandwidth.  You implement bandwidth throttling so that it only uses a specified percentage (or some other calculated amount) of the total bandwidth - thus all other applications that are working across that pipe can still function at the same time without too serious of a performance hit.
one option would be to run your process in another thread, and set the priority to low or lowest. that way, all other apps respond as if it wasn't running at all. i wrote a backup software program where i did that.
Avatar of stumpy1

ASKER

Its nothing to do with task priority - Its bandwidth that I need throttled as opposed to the processor.
I need to put some code in the program so that it wont eat up all the network bandwidth while doing its copies, moves etc.
yes, i understood your problem... but if you were after making sure the other applications running were as responsive as possible, this would be an added step to attain that goal.
ASKER CERTIFIED SOLUTION
Avatar of CraigHarris
CraigHarris

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of stumpy1

ASKER

CraigHarris,
Its not really what I had in mind but I like concept.  I tested out your idea but modified it so I could be used on all types of files, text, data, binaries, executables etc.  I probably wont end up using it - but I will file it for use at some time in the future.
This is the code: (replace file names).

            {
                  int kbps = 100; // peak kilobytes per second to transfer - average will be a bit less
                  FileStream fs = new FileStream(@"\\remote-server\folder\filename", FileMode.Open, FileAccess.Read);
                  
                  // Create the reader
                  BinaryReader r = new BinaryReader(fs);
                  FileStream fs2 = new FileStream(@"C:\folder\filename", FileMode.CreateNew);

                  // Create the writer for data.
                  BinaryWriter w = new BinaryWriter(fs2);
                  byte[] buffer;

                  for (int i = 0; i < fs.Length; i+=1024)
                  {
                        // Read data into buffer from input file.
                        buffer = r.ReadBytes(1024);
                        // Write data to òutput file.
                        w.Write(buffer);
                        System.Threading.Thread.Sleep(1000/kbps); //appropriate delay to restrict throughput
                  }
                  r.Close();
                  w.Close();
                  fs.Close();
                  fs2.Close();
            }
Avatar of stumpy1

ASKER

Thanks for all the help
This project seems to be aimed at solving the exact problem you were working on:

http://www.codeproject.com/KB/IP/Bandwidth_throttling.aspx