Search code examples
c#networkingdownload-speed

Get Download and Upload Speeds C#


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


Solution

  • 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.