InetAddress
InetAddress: 用于标志网络上的硬件资源(表示IP地址的相关信息)
1 public class InetAddressTest { 2 3 public static void main(String[] args) throws UnknownHostException { 4 // 获取本机的intAddres实例 5 InetAddress address = InetAddress.getLocalHost();//得到InetAddress实例 6 System.out.println("本机IP地址:"+address.getHostAddress());//获取本机的IP地址 7 System.out.println("获取本机名:"+address.getHostName());//获取本机名 8 byte[] bytes = address.getAddress();//获取字节数组形式的IP地址 9 System.out.println(Arrays.toString(bytes)); 10 System.out.println(address);//lah-PC/192.168.56.1 11 12 InetAddress address2 = InetAddress.getByName("lah-pc"); 13 //InetAddress address2 = InetAddress.getByAddress(bytes); 14 System.out.println("本机IP地址:"+address2.getHostAddress());//获取本机的IP地址 15 System.out.println("获取本机名:"+address2.getHostName());//获取本机名 16 17 } 18 19 }