package com.test.ping;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;
public class TestPing {
private static String IP = "12.6.6.6";
public static void main(String args[]){
while(!ping());
}
private static boolean ping(){
try {
SocketAddress address = new InetSocketAddress(IP, 8082);
Socket socket = new Socket();
socket.connect(address, 2 * 1000);
socket.close();
return true;
} catch (Exception e) {
//e.printStackTrace();
System.out.println("链接失败");
return false;
}
}
}
2秒内如果连不上则判断为超时、无法连通,一直到能够连通为止则跳出循环。