java 對 URL訪問、校驗埠是否佔用、IP是否ping通
public class Shishi { private Logger log = Logger.getLogger(Shishi.class); /** 當返回值是true時,說明host是可用的,false則不可。*/
public static boolean ping(String ipAddress) throws Exception { int timeOut = 3000; // 超時應該在3鈔以上 boolean status = InetAddress.getByName(ipAddress).isReachable(timeOut); return status; } private static boolean validePort(String location, int port) { Socket s = new Socket(); try { SocketAddress add = new InetSocketAddress(location, port); s.connect(add, 2000); return true; } catch (IOException e) { return false; }finally{ try { s.close(); } catch (IOException e1) { } } } public static void main(String[] args) { try {
System.out.println(validePort("127.0.0.1",8080)); System.out.println(ping("127.0.0.1")); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }