InetAddress

InetAddress

InetAddress 主要用于标示 IP 地址,这个类有两个子类:Inet4Address、Inet6Address,分别标示IPv4 和IPv6。

范例1:

  1. package haizhu.com.InetDemo;  
  2.   
  3. import java.net.InetAddress;  
  4.   
  5. public class InetAddressDemo {  
  6.     public static void main(String[] args) throws Exception{  
  7.         InetAddress locAdd = null;  
  8.         InetAddress remAdd = null;  
  9.         locAdd = InetAddress.getLocalHost();                            //得到本地InetAddress对象  
  10.         remAdd = InetAddress.getByName("www.baidu.com");                //取得远程InetAddress对象  
  11.         System.out.println("本机IP地址:"+locAdd.getHostAddress());          //得到本地IP地址  
  12.         System.out.println("百度IP地址:"+remAdd.getHostAddress());          //得到百度IP地址  
  13.         System.out.println("本机是否可达:"+locAdd.isReachable(5000));  
  14.     }  
  15. }  

结果:

  1. 本机IP地址:192.168.1.109  
  2. 百度IP地址:115.239.210.26  
  3. 本机是否可达:true  
原文地址:https://www.cnblogs.com/signheart/p/6597972.html