62,614
社区成员
发帖
与我相关
我的任务
分享
import java.io.IOException;
import java.net.InetAddress;
import java.util.concurrent.Executors;
public class Pinger implements Runnable {
public static void main(String[] args) throws Exception {
Executors.newCachedThreadPool().submit(new Pinger("127.0.0.1", 3000));
}
private final String ip;
private final int timeout;
public Pinger(String ip, int timeout) {
this.ip = ip;
this.timeout = Math.max(1000, timeout);
}
@Override
public void run() {
try {
while(true) {
InetAddress inet = InetAddress.getByName(ip);
System.out.print(ip);
if( inet.isReachable(timeout) )
System.out.println("连接成功");
else
System.out.println("连接失败");
}
}
catch(IOException ex) {
ex.printStackTrace();
}
}
}