I want to get the ping execution time and result in string after ping host

前端 未结 4 1980
醉梦人生
醉梦人生 2021-01-13 14:10

I want to get the ping execution time and result in string after ping host. How can I do it?

4条回答
  •  北海茫月
    2021-01-13 14:15

    long currentTime = System.currentTimeMillis();
    boolean isPinged = InetAddress.getByName(servername).isReachable(2000); // 2 seconds
    currentTime = System.currentTimeMillis() - currentTime;
    if(isPinged) {
        System.out.println("pinged successfully in "+ currentTime+ "millisecond");
    } else {
        System.out.println("PIng failed.");
    }
    

    But this will use ICMP ping only in windows system.

提交回复
热议问题