InetAddress类的提供了一些对于IP地址的很多操作。
下面简单演示了几种用法,用到查API就好了。
public static void main(String[] args) throws Exception
{
//返回本地主机的IP地址对象
//InetAddress address = InetAddress.getLocalHost();
//获取任意一台主机的IP地址对象
InetAddress address = InetAddress.getByName("www.baidu.com");
//获取ip地址
String ip = address.getHostAddress();
//获取主机名
String name = address.getHostName();
System.out.println(ip+":"+name);
}