【问题标题】:How to get the time interval a ping request is sent and received如何获取发送和接收 ping 请求的时间间隔
【发布时间】:2014-06-26 01:27:07
【问题描述】:

我需要为我们正在使用的模拟器运行一些测试,为此我需要测试指定的 RTT 延迟。有时 RTT 延迟会增加 10 倍以上,在那些时候,我需要测试这在现实中是否正确。例如,如果模拟器的日志在时间 X 显示 RTT 设置为 400 毫秒,那么我可以看到验证使用连接到模拟器网络的真实应用程序。最好的方法是使用带有时间戳的 ping。有什么方法可以指定发送 ping 请求的时间以及延迟是多少?或者随时建议我的测试用例的任何其他方法。

我有一个 Java 实现只是为了发送 ping 请求!

public static void main(String[] args) throws UnknownHostException, IOException {
    String ipAddress = "127.0.0.1";
    InetAddress inet = InetAddress.getByName(ipAddress);

    System.out.println("Sending Ping Request to " + ipAddress);
    System.out.println(inet.isReachable(5000));
}

【问题讨论】:

  • 考虑调用 isReachable 并不断增加超时值,直到成功。

标签: java ping


【解决方案1】:
String ipAddress = "127.0.0.1";
InetAddress inet = InetAddress.getByName(ipAddress);   
System.out.println("Sending Ping Request to " + ipAddress);
int uptime = (int) System.currentTimeMillis();
System.out.println((inet.isReachable(5000) ? ((int) System.currentTimeMillis() -    uptime)+"ms" : "Request timed out"));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-25
    • 1970-01-01
    • 2013-08-05
    相关资源
    最近更新 更多