获取IP地址

 public static void main(String[] args) throws IOException {
          try {
               InetAddress[] addresses2 = InetAddress.getAllByName("admin");//获取本机所有IP地址
               for(InetAddress addr:addresses2){
                   System.out.println(addr);
               }
               InetAddress address = InetAddress.getLocalHost();//获取的是本地的IP地址 
               String hostAddress = address.getHostAddress();//192.168.0.121   
               System.out.println(hostAddress);
               
               InetAddress address1 = InetAddress.getByName("www.cnblogs.com");//根据网站地址获取网站的IP
               String hostAddress1 = address1.getHostAddress();//获取网站IP
               String hostName=address1.getHostName();//获取服务器名
               boolean  sock=address1.isReachable(3000);//是否可以到达该地址
               System.out.println(hostAddress1);//
               System.out.println(hostName);
               System.out.println("Reach: " + sock);
               InetAddress[] addresses = InetAddress.getAllByName("www.baidu.com");//根据主机名返回其可能的所有InetAddress对象 
               for(InetAddress addr:addresses){ 
               System.out.println(addr);
               } 
               
         } catch (UnknownHostException e) { 
              e.printStackTrace();
         } 
      }
View Code
原文地址:https://www.cnblogs.com/feitianshaoxai/p/6889728.html