java 如何测试互联网速度(JavaSE)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2490552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to test internet speed (JavaSE)?
提问by Mr CooL
Is there a way to test internet speed using Java code?
有没有办法使用 Java 代码测试互联网速度?
For instance, like how we actually test with cmd command, ping command.
例如,就像我们实际使用 cmd 命令、ping 命令进行测试一样。
Thanks in advance...
提前致谢...
回答by albertjan
Look here on howto ping in java:
看这里如何在java中ping:
http://blog.taragana.com/index.php/archive/how-to-do-icmp-ping-in-java-jdk-15-and-above/
http://blog.taragana.com/index.php/archive/how-to-do-icmp-ping-in-java-jdk-15-and-above/
As the other dude said it is not a perfect solution I looked into our codebase because I knew we do something with ICMP packets here's what we use:
正如另一个人所说,这不是一个完美的解决方案,我查看了我们的代码库,因为我知道我们对 ICMP 数据包做了一些事情,这就是我们使用的:
http://www.savarese.com/software/rocksaw/
http://www.savarese.com/software/rocksaw/
It does mean you'll have to compile a bit of JNI C to have access to a rawsocket (win32 and linux) are supported.
这确实意味着您必须编译一些 JNI C 才能访问支持原始套接字(win32 和 linux)。
But you should look into a few more things to get a propper idea of internet speed:
但是您应该查看更多内容以正确了解互联网速度:
- check the amount of connections possible in 10 secs
- check how long it takes to download a 5MB file
- check how long it takes to upload a 5MB file
- 检查 10 秒内可能的连接数量
- 检查下载 5MB 文件需要多长时间
- 检查上传 5MB 文件需要多长时间
that in combination with a few pings should give you quite a good impression of bandwidth and latency
结合几个 ping 应该会给你一个很好的带宽和延迟印象
回答by Favas kottayil
use JSpeedTestLibrary
使用JSpeedTest库
SpeedTestSocket speedTestSocket = new SpeedTestSocket();
// add a listener to wait for speedtest completion and progress
speedTestSocket.addSpeedTestListener(new ISpeedTestListener() {
@Override
public void onCompletion(SpeedTestReport report) {
// called when download/upload is complete
System.out.println("[COMPLETED] rate in octet/s : " + report.getTransferRateOctet());
System.out.println("[COMPLETED] rate in bit/s : " + report.getTransferRateBit());
}
@Override
public void onError(SpeedTestError speedTestError, String errorMessage) {
// called when a download/upload error occur
}
@Override
public void onProgress(float percent, SpeedTestReport report) {
// called to notify download/upload progress
System.out.println("[PROGRESS] progress : " + percent + "%");
System.out.println("[PROGRESS] rate in octet/s : " + report.getTransferRateOctet());
System.out.println("[PROGRESS] rate in bit/s : " + report.getTransferRateBit());
}
});

