import java.net.InetAddress;
import java.net.UnknownHostException;
public class InetAddressDemo {
public static void main(String args[])throws UnknownHostException{
// fun();
fun_1();
}
public static void fun_1() throws UnknownHostException
// 获取主机名IP地址
InetAddress inet = InetAddress.getByName("www.baidu.cn");
System.out.println(inet);
}
public static void fun() throws UnknownHostException{
//获取本机的主机名和IP地址
InetAddress inet = InetAddress.getLocalHost();
System.out.println(inet);
// 获取本机的IP地址
String add = inet.getHostAddress();
// 获取本机的主机名
String name = inet.getHostName();
System.out.println(add+" "+name);
}
}
原文:http://www.cnblogs.com/mumunuo/p/7214551.html