获取IP地址
Posted 一念花开满天下
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了获取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(); } }
以上是关于获取IP地址的主要内容,如果未能解决你的问题,请参考以下文章