- package org.renlr.p2p;
- import java.net.InetAddress;
- public class Test {
- public static void main(String[] args) {
- String IP = "10.1.11.225";
- if (Test.ping(IP))
- System.out.println("SUCCESS - ping " + IP + " with no interface specified");
- else
- System.out.println("FAILURE - ping " + IP + " with no interface specified");
- }
- /**
- *
- * @param host 主机地址
- *
- * @return boolean
- * */
- public static boolean ping(String host) {
- String $host = host;
- try {
- InetAddress address = null;
- if ($host != null && $host.trim().length() > 0) {
- address = InetAddress.getByName($host);
- }
- if (address != null) {
- } else {
- System.out.println($host + " is unrecongized");
- }
- if (address.isReachable(5000))
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- }
- return false;
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/250420149375.html
来源: http://www.codesnippet.cn/detail/250420149375.html