Download and upload c # speeds

I am looking for a class or library or something that will allow me to get the current download speed, I tried a lot of code from the network, including FreeMeter, but I can not get it to work.

Can someone provide any code just to provide this simple functionality.

thanks a lot

+4
source share
2 answers

I assume you want kb / sec. This is determined by taking kbreceived and dividing it by current seconds minus the initial seconds. I'm not sure how to make a DateTime for this in C #, but in VC ++ it will be like this:

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

Then you split:

 double kbsec = kbreceived / secs; 

To get kbreceived , you need to take currentBytes to read, add bytes already read, and 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 specific implementation functions, the basic concept is the same regardless of language.

+1
source

If you need current download and download speeds, here's how:

Make a 1 second interval timer, if you want it to be updated with this interval, your choice. At the timer mark, 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 this solves. If you have a different time interval for the timer, just divide the time you gave the MAGNITUDE we gave.

+1
source

Source: https://habr.com/ru/post/1299990/


All Articles