/** * Created by LiuHuiChao on 2016/3/9. * InetAddress类用于表示网络上的硬件资源,表示互联网协议(IP)地址 * */ public class InetAddressTest { public static void main(String[] args) throws UnknownHostException{ //获取本机的InetAddress实例 InetAddress address=InetAddress.getLocalHost(); System.out.println("当前计算机名称:"+address.getHostName()); System.out.println("IP地址:"+address.getHostAddress()); byte[] bytes=address.getAddress();//获取字节数组形式的IP地址 System.out.println("字节数组形式的IP:"+ Arrays.toString(bytes)); System.out.println(address);//直接输出InetAddress对象 System.out.println("------------分割线-----------------------------------"); //根据机器名,获取InetAddress实例 //InetAddress address2=InetAddress.getByName("ljw-pc"); InetAddress address2=InetAddress.getByName("192.168.21.50"); System.out.println("当前计算机名称:"+address2.getHostName()); System.out.println("IP地址:"+address2.getHostAddress()); } }