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
LVL 5
stumpy1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gbzhhuCommented:
what is bandwidth throttling?
0
stumpy1Author Commented:
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.
0
msdixonCommented:
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.
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

stumpy1Author Commented:
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.
0
msdixonCommented:
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.
0
CraigHarrisCommented:
You can do this in C#, but you will have to copy blocks of data to a stream, and write your own logic to restrict the speed. I don't believe any built-in classes offer such.

Use something like:

        kbps = 100; // peak kilobytes per second to transfer - average will be a bit less
        StreamWriter sw = new StreamWriter(@"\\remote-server\folder", true, Encoding.UTF8, 1024);
        sw.AutoFlush = true;
        StreamReader sr = new StreamReader(@"c:\local.file");
        char[] buffer;
        int index;
        while(true) {          
          sr.ReadBlock(buffer, index, 1024); // read 1kb
          sw.Write(buffer);//write 1kb
          System.Threading.Thread.Sleep(1000/kbps); //appropriate delay to restrict throughput
        }


Please note that code is a quick example and is not tested what-so-ever, but it is adaptable to achieve your solution.

You will either need a thread per transfer, or interleave the transfers in your own logic -- either will work fine.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
stumpy1Author Commented:
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();
            }
0
stumpy1Author Commented:
Thanks for all the help
0
trey_carrollCommented:
This project seems to be aimed at solving the exact problem you were working on:

http://www.codeproject.com/KB/IP/Bandwidth_throttling.aspx
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.