InetAddress
InetAddress
InetAddress 主要用于标示 IP 地址,这个类有两个子类:Inet4Address、Inet6Address,分别标示IPv4 和IPv6。
范例1:
- package haizhu.com.InetDemo;
- import java.net.InetAddress;
- public class InetAddressDemo {
- public static void main(String[] args) throws Exception{
- InetAddress locAdd = null;
- InetAddress remAdd = null;
- locAdd = InetAddress.getLocalHost(); //得到本地InetAddress对象
- remAdd = InetAddress.getByName("www.baidu.com"); //取得远程InetAddress对象
- System.out.println("本机IP地址:"+locAdd.getHostAddress()); //得到本地IP地址
- System.out.println("百度IP地址:"+remAdd.getHostAddress()); //得到百度IP地址
- System.out.println("本机是否可达:"+locAdd.isReachable(5000));
- }
- }
结果:
- 本机IP地址:192.168.1.109
- 百度IP地址:115.239.210.26
- 本机是否可达:true