码迷,mamicode.com
首页 > 其他好文 > 详细

获取IP地址

时间:2017-05-22 16:49:59      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:stack   view   logs   throws   获取   sre   src   pen   blog   

技术分享
 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

 

获取IP地址

标签:stack   view   logs   throws   获取   sre   src   pen   blog   

原文地址:http://www.cnblogs.com/feitianshaoxai/p/6889728.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!