How to measure internet bandwidth

I have a problem and cannot find the answers. I would like to measure internet bandwidth using java, but I don't know how to do this.

It would be great to get some tips; I know that I need to open a socket and send it to a specific server, return it and then use the time.

But how would this be done?

+3
source share
2 answers

Well, I would implement this by simply uploading a file with a fixed size. Not tested, but something in this direction should work fine.

byte[] buffer = new byte[BUFFERSIZE];
Socket s = new Socket(urlOfKnownFile);
InputStream is = s.getInputStream();
long start = System.nanoTime();
while (is.read(buffer) != -1) continue;
long end = System.nanoTime();
long time = end-start;
// Now we know that it took about time ns to download <filesize>. 
// If you don't know the correct filesize you can obviously use the total of all is.read() calls.
+5
source

How about fixing an arbitrary amount of time and sending data related to it?

, , , 100Bytes/s. 1 , 1 100 .

, , :

timer_get (a);
sent_data = 0;

while (not_finished_sending_data)
{
    timer_get (b);
    if ((b - a) < 1 ) // 1 second
    {
        if (sent_data < 100) // 100 bytes
        {
            // We actually send here
            sent_data += send();
        }
    }
    else
    {
        timer_get (a);
        sent_data = 0;
    }
}
+1

All Articles