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();
}
}
}