其他

检测DNS

2021-08-27  本文已影响0人  大旺旺的弟弟小旺旺

使用网络地址获取主机名:

InetAddress.getByName(this.domain);获取主机地址

如果可以得到就说明是好的。
检测的代码

public class TestDns implements Runnable {
    private String hostName;
    private InetAddress inetAddr;

    public TestDns(String hostName) {
        this.hostName = hostName;
    }

    public void run() {
        try {
            set(InetAddress.getByName(this.hostName));
        } catch (UnknownHostException e) {
        }
    }

    public synchronized void set(InetAddress inetAddr2) {
        this.inetAddr = inetAddr2;
    }

    public synchronized InetAddress get() {
        return this.inetAddr;
    }
}

使用一个线程来等待

 TestDns dnsRes = new TestDns(hostname);
 Thread t = new Thread(dnsRes);
 t.start();
 t.join((long) timeout);
 InetAddress inetAddr = dnsRes.get();
 System.out.println(inetAddr);
上一篇 下一篇

猜你喜欢

热点阅读