How to detect Internet connection speed with Java?

17,393

Solution 1

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.

Solution 2

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!

Solution 3

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.

Share:
17,393
Frank
Author by

Frank

I've been a Java developer for over 20 years. Recently got a patent for "Interception-resistant Authentication System And Method" : GATE [ Graphic Access Tabular Entry ]. GATE makes user passwords much safer, it helps you to defeat peeking, keylogging, phishing  and dictionary attack. Info : https://gatecybertech.com Demo : https://gatecybertech.net GATE has won multiple cyber security awards. If any organization wants to license it, please contact me.

Updated on June 25, 2022

Comments

  • Frank
    Frank almost 2 years

    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.

    • FrustratedWithFormsDesigner
      FrustratedWithFormsDesigner over 13 years
      I think for this to truly work you'd have to have a server on the other end of your test that can handle speeds higher than the maxmimum limit of your AT&T connection...
    • Randolpho
      Randolpho over 13 years
      Even if you could get the "official" connection speed for the user's internets, that number would be wildly inaccurate for most real-world scenarios. Perhaps you should reconsider exactly why you need this information and find a different way to do whatever it is you're really trying to do.
    • nos
      nos over 13 years
      Are your "streams" bandwidth limited, or will you end up with 1 stream dowloading as fast as possible vs 10 streams downloading as fast as possible (in which case you barely gain anything)
    • Randolpho
      Randolpho over 13 years
      Based on your edit, I'd say your best bet is to let the user configure the number of parallel streams to use. I assume you're doing some form of file sharing app? How would you feel if BitTorrent just automatically chose how many outgoing or incoming connections it would allow?
  • Randolpho
    Randolpho over 13 years
    Alas, that would only work to determine the connection speed to the server from which the file was downloaded, not the general link-level transfer speed that the original poster appears to desire.
  • FrustratedWithFormsDesigner
    FrustratedWithFormsDesigner over 13 years
    ...yeah, what Randolpho said.
  • Reese Moore
    Reese Moore over 13 years
    @Randolpho: You would do it for the server which you would be communicating with. All other link speeds are irrelevant.
  • Frank
    Frank over 13 years
    Thanks, that's one clever way to skin a cat !
  • Randolpho
    Randolpho over 13 years
    I agree, but as I pointed out, the original asker wanted the link-level speed -- something nobody writing a non-driver should care about.