3

I'm looking for a class or a library or anything that will allow me to get the current download speed, I've tried a lot of code from the net including FreeMeter but can't get it to work.

Can some provide any sort of code just to give this simple functionality.

Thanks a lot

Sandeep Bansal
  • 6,280
  • 17
  • 84
  • 126
  • What are you trying to achieve? Measure the speed of the pipe as a whole, or are you trying to get the speed of a download you are performing? – Matthew Scharley Feb 01 '10 at 21:20
  • I'm basically trying to get the usage of the network on the system, if it is idle or if there is any downloads going on etc. just usage statistics. – Sandeep Bansal Feb 03 '10 at 23:15

3 Answers3

1

If you want Current Download and Upload Speed, here is how :

Make a timer of interval 1 second, if you want it to update at that interval, your choice. On the timers tick, add this code :

using System.Net.NetworkInformation;

int previousbytessend = 0;
int previousbytesreceived = 0;
int downloadspeed;
int uploadspeed;
IPv4InterfaceStatistics interfaceStats;
private void timer1_Tick(object sender, EventArgs e)
    {

        //Must Initialize it each second to update values;
        interfaceStats = NetworkInterface.GetAllNetworkInterfaces()[0].GetIPv4Statistics();

        //SPEED = MAGNITUDE / TIME ; HERE, TIME = 1 second Hence :
        uploadspeed = (interfaceStats.BytesSent - previousbytessend) / 1024; //In KB/s
        downloadspeed = (interfaceStats.BytesReceived - previousbytesreceived) / 1024;

        previousbytessend= NetworkInterface.GetAllNetworkInterfaces()[0].GetIPv4Statistics().BytesSent;
        previousbytesreceived= NetworkInterface.GetAllNetworkInterfaces()[0].GetIPv4Statistics().BytesReceived;

        downloadspeedlabel.Text = Math.Round(downloadspeed, 2) + " KB/s"; //Rounding to 2 decimal places
        uploadspeedlabel.Text = Math.Round(uploadspeed, 2) + "KB/s";
    }

I guess that solves it. If you have different time interval for the timer, just divide the time you gave with the MAGNITUDE we have given.

fahimalizain
  • 844
  • 8
  • 15
1

I'm guessing you want kb/sec. That is determined by taking kbreceived and dividing it by the current seconds minus the starting seconds. I'm not sure how to do the DateTime for this in C#, but in VC++ it would be like so:

COleDateTimeSpan dlElapsed = COleDateTime::GetCurrentTime()
                           - dlStart;
secs = dlElapsed.GetTotalSeconds();

You then divide:

double kbsec = kbreceived / secs;

To get kbreceived, you need to take the currentBytes read, add in bytes already read, then divide by 1024.

So,

   // chunk size 512.. could be higher up to you

   while (int bytesread = file->Read(charBuf, 512))
   {
        currentbytes = currentbytes + bytesread;
        // Set progress position by setting pos to currentbytes
   }



   int percent = currentbytes * 100 / x ( our file size integer
                               from above);
   int kbreceived = currentbytes / 1024;

Minus some implementation specific functions, the basic concept is the same regardless of language.

0

Fix for the solution above:

  1. Use using System.Net.NetworkInformation;

  2. Write Down:

        long previousbytessend = 0;
        long previousbytesreceived = 0;
        long downloadspeed;
        long uploadspeed;
        IPv4InterfaceStatistics interfaceStats;

        private void internetSpeed_Tick(object sender, EventArgs e)
        {
            interfaceStats = NetworkInterface.GetAllNetworkInterfaces()[0].GetIPv4Statistics();

            uploadspeed = (interfaceStats.BytesSent - previousbytessend) / 1024; 
            downloadspeed = (interfaceStats.BytesReceived - previousbytesreceived) / 1024;

            previousbytessend = NetworkInterface.GetAllNetworkInterfaces()[0].GetIPv4Statistics().BytesSent;
            previousbytesreceived = NetworkInterface.GetAllNetworkInterfaces()[0].GetIPv4Statistics().BytesReceived;

            txtDownloaded.Text = Math.Round(((double)downloadspeed), 2) + " KB/s";
            txtUploaded.Text = Math.Round(((double)uploadspeed), 2) + "KB/s";
        }

There were about 5 errors using the solution above this answer, however I was able to fix it by changing the definitions to longs instead of ints and converting the decimals into doubles in the Math.Round() part.

If you are having problems with Performance, I'd recommend you use the BackgroundWorker control.

Pronner
  • 25
  • 1
  • 9