如何用Java检测互联网连接速度?

发布于 2024-10-01 09:29:33 字数 293 浏览 0 评论 0原文

在我的 Java 应用程序中,如何检测互联网连接速度有多快?例如,我在家使用 AT&T Fast DSL,我想知道是否有一种方法可以编写一个执行以下操作的方法:

int getInternetConnectionSpeed()
{
   ...
}

它将返回一个以 kbps 为单位的数字,例如 2800kbps [ 2.8 M ]

编辑: 我问的原因是在我的应用程序中,我可以打开多个互联网流,具体取决于用户的互联网连接速度,我希望它自动确定要打开多少个流,而不会阻塞应用程序。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北斗星光 2024-10-08 09:29:33

我认为你可能以错误的方式思考这个问题。拍摄连接速度快照仅表明该时刻的吞吐量。当您运行测试时,他们很容易运行另一个应用程序,这会消耗他们的带宽,然后您的测量值就毫无价值了。

相反,我认为您应该不断添加或删除线程,具体取决于线程的吞吐量是增加还是减少。我建议这样的事情(仅限伪代码):

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);
      }
    }
  }
}

您可以调整睡眠时间以适应。我在这里假设 getCurrentSpeed() 返回所有下载线程的吞吐量,并且您能够在应用程序执行期间动态打开和关闭线程。

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.

清音悠歌 2024-10-08 09:29:33

计算下载一个已知(且足够大)大小的文件需要多长时间。

如果下载 10MB 需要 60 秒,则您拥有 (10 * 1024 * 8 / 60) Kbps 或 1365 Kbps 连接。

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.

愿得七秒忆 2024-10-08 09:29:33

但有很多速度取决于您要连接到的位置:

  • 127.0.0.1?
  • 您的本地子网?
  • 你的互联网?

由于您的 JVM 使用使用本地网络的本地 PC,因此无法自动获取 DSL 速度。

哦,请注意,您甚至可能正在长距离冲浪!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文