Sign in to follow this  

Calculate download speed with Recv?

Recommended Posts

[quote name='Anddos' timestamp='1335607761' post='4935554']
How do you calculate the download speed when recieving data with recv?

recv returns the amount of data read in bytes, add up the total amount of data recieved across multiple calls and divide by the time it took to recieve that data.

Share this post

Link to post
Share on other sites
[quote name='Anddos' timestamp='1335610911' post='4935560']
how do you get the time it took?

it depends on your OS, recv itself doesn't take any time at all.

Basically you do something like this:
startime = getCurrentTimeFromOS();
currenttime = starttime;
lastintervalTime = starttime;
totalBytesdownloaded = 0;
byteslastSecond =0;
while we're not done with the download {
bytesThisRecv = recv(....);
currentTime = getCurrentTimeFromOS();
if (currentTime > lastIntervalTime + 1 second) {
downloadSpeedLastSecond = bytesLastSecond; //this is the average speed across the last second
bytesLastSecond = 0;
lastIntervalTime+= 1 second;
averageSpeed = totalBytes / (currenttime - startime); //this is the total average speed for the download

on Windows you can use timeGetTime to get the current time in milliseconds (~10 ms accuracy normally which should be good enough for measuring download speed) or queryPerformanceCounter for a very high accuracy timer, on Linux you use clock_gettime Edited by SimonForsman

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this