java网络编程-获取域名和ip地址

时间:2021-08-15 04:04:16
package com.zhiru;

import java.net.InetAddress;
import java.net.UnknownHostException;

/*
* 域名和ip地址获取
* Java中用InetAddress类获取域名和ip地址.
*/
public class DNSAndIPAddress {
public DNSAndIPAddress(){

}
//根据域名获取ip地址
public static InetAddress getIP(String dns) throws UnknownHostException{
return InetAddress.getByName(dns);
}
//根据ip地址获取域名
public static InetAddress getDNS(String ip) throws UnknownHostException{
return InetAddress.getByName(ip);
}
//获取本机的域名和ip地址
public static String getHostAddress() throws UnknownHostException{
return InetAddress.getLocalHost().toString();
}
public static void main(String[] args) throws UnknownHostException{
System.out.println(getIP("www.upc.edu.cn").toString());
System.out.println(getDNS("www.baidu.com").toString());
System.out.println(getDNS("www.csdn.net").toString());
System.out.println(getDNS("www.cnblogs.com").toString());
System.out.println(getDNS("www.importnew.com").toString());
System.out.println(getDNS("zhirublog.lingw.net").toString());
//System.out.println(getDNS("blog.csdn.net/zhiruchen").toString());
System.out.println(getHostAddress());
}
}
www.upc.edu.cn/121.251.255.36
www.baidu.com/119.75.218.77
www.csdn.net/117.79.157.225
www.cnblogs.com/42.121.252.58
www.importnew.com/106.186.122.244
zhirublog.lingw.net/60.191.221.85