如何用Java检测互联网连接速度?
在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为你可能以错误的方式思考这个问题。拍摄连接速度快照仅表明该时刻的吞吐量。当您运行测试时,他们很容易运行另一个应用程序,这会消耗他们的带宽,然后您的测量值就毫无价值了。
相反,我认为您应该不断添加或删除线程,具体取决于线程的吞吐量是增加还是减少。我建议这样的事情(仅限伪代码):
您可以调整睡眠时间以适应。我在这里假设
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):
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.计算下载一个已知(且足够大)大小的文件需要多长时间。
如果下载 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.
但有很多速度取决于您要连接到的位置:
由于您的 JVM 使用使用本地网络的本地 PC,因此无法自动获取 DSL 速度。
哦,请注意,您甚至可能正在长距离冲浪!
But there are many speeds depending on where you want to connect to:
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!