Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect Internet connection speed with Java?

In my Java app, how do I detect how fast the Internet connection speed is ? For instance, I use AT&T Fast DSL at home, I wonder if there is a way I can write a method that does the following :

int getInternetConnectionSpeed()
{
   ...
}

Which will return a number in kbps, something like 2800kbps [ 2.8 M ]

Edit : The reason I'm asking, is in my app, I can open multiple Internet streams, depending on the users' Internet connection speed, I want it to auto determine how many streams to open without chocking the app.

like image 344
Frank Avatar asked Nov 08 '10 20:11

Frank


People also ask

How can I check my Internet speed in Java?

Checking Internet connectivity using Java can be done using 2 methods: 1) by using getRuntime() method of java Runtime class. 2) by using methods of java URL and URLConnection classes.

How can I check my Internet speed using CMD?

In Windows, click the start menu and type "CMD" to access the command prompt. Type "ping google.com" and choose "enter" to execute the function and retrieve a list of results. The results have several lines of data that show the speed along with other metrics.


3 Answers

I think that you could be thinking about the problem in the wrong way. Taking a snapshot of a connection speed is only an indication of their throughput at that instant in time. They could quite easily be running another application when you run test that sucks their bandwidth and then your measured values are worthless.

Instead, I think you should be constantly adding or removing threads depending on whether it increases or decreases their throughput. I'd suggest something like this (pseudo code only):

while(true) {
  double speedBeforeAdding = getCurrentSpeed();
  addThread();
  // Wait for speed to stabilise
  sleep(20 seconds);
  double speedAfterAdding = getCurrentSpeed();
  if(speedAfterAdding < speedBeforeAdding) {
    // Undo the addition of the new thread
    removeThread();
    // Wait for speed to stabilise
    sleep(20 seconds);

    if(getNumberOfThreads() > 1) {
      double speedBeforeRemoving = getCurrentSpeed();
      // Remove a thread because maybe there's too many
      removeThread();
      // Wait for speed to stabilise
      sleep(20 seconds);
      double speedAfterRemoving = getCurrentSpeed();
      if(speedAfterRemoving < speedBeforeRemoving) {
        // Add the thread back
        addThread();
        // Wait for speed to stabilise
        sleep(20 seconds);
      }
    }
  }
}

You can fiddle with the sleep timings to suit. I'm assuming here that getCurrentSpeed() returns the throughput of all download threads and that you're able to dynamically open and close threads during your application's execution.

like image 53
Catchwa Avatar answered Oct 23 '22 04:10

Catchwa


Time how long it takes you to download a file of known (and sufficiently large) size.

If it takes you 60s to download 10MB, you have a (10 * 1024 * 8 / 60) Kbps or 1365 Kbps connection.

like image 30
Reese Moore Avatar answered Oct 23 '22 05:10

Reese Moore


But there are many speeds depending on where you want to connect to:

  • 127.0.0.1?
  • Your local subnet?
  • Your internet?

Since your JVM uses the local PC which uses the local net there is no way to get the DSL speed automatically.

Oh, and please notice, you might even be surfing long distance!

like image 24
Daniel Avatar answered Oct 23 '22 04:10

Daniel