Note that there are some explanatory texts on larger screens.

plurals
  1. POCalculating network download speed
    primarykey
    data
    text
    <p>I have written the follwing code to calculate download speed using java. But it is not giving correct results.What is the problem?.Is there a problem with my logic , or is it a problem with java networking classes usage?I think it is a problem with the usage of java networking classes.Can anybody tell me what exactly the problem is?</p> <pre><code>/*Author:Jinu Joseph Daniel*/ import java.io.*; import java.net.*; class bwCalc { static class CalculateBw { public void calculateUploadBw() {} public float calculateDownloadRate(int waitTime) throws Exception { int bufferSize = 1; byte[] data = new byte[bufferSize]; // buffer BufferedInputStream in = new BufferedInputStream(new URL("https://www.google.co.in/").openStream()); int count = 0; long startedAt = System.currentTimeMillis(); long stoppedAt; float rate; while (((stoppedAt = System.currentTimeMillis()) - startedAt) &lt; waitTime) { if ( in .read(data, 0, bufferSize) != -1) { count++; } else { System.out.println("Finished"); break; } } in .close(); rate = 1000 * (((float) count*bufferSize*8 / (stoppedAt - startedAt)) )/(1024*1024);//rate in Mbps return rate; } public float calculateAverageDownloadRate() throws Exception{ int times[] = {100,200,300,400,500}; float bw = 0,curBw; int i = 0, len = times.length; while (i &lt; len) { curBw = calculateDownloadRate(times[i++]); bw += curBw; System.out.println("Current rate : "+Float.toString(curBw)); } bw /= len; return bw; } } public static void main(String argc[]) throws Exception { CalculateBw c = new CalculateBw(); System.out.println(Float.toString(c.calculateAverageDownloadRate())); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload