package com.haoran.lesson; import java.net.InetAddress; import java.net.UnknownHostException; public class testip { public static void main(String[] args) throws UnknownHostException { InetAddress byName1 = InetAddress.getByName("www.baidu.com");//查询目标网址的IP地址 System.out.println(byName1); InetAddress byName2 = InetAddress.getByName("localhost");//查询本机网址的IP地址 System.out.println(byName2); System.out.println(byName1.getCanonicalHostName());//获得规范的名字 System.out.println(byName1.getHostAddress());//ip System.out.println(byName1.getHostName());//域名 } }