/** * @BelongsProject: idea * @BelongsPackage: day02_socket * @Author: minwen XIE * @CreateTime: 2022-10-26 10:43 * @Description: TODO * @Version: 1.0 */ public class InetAddressDemo { public static void main(String[] args) throws Exception{ //获取主机基本信息 InetAddress localHost = InetAddress.getLocalHost(); System.out.println(localHost); //获取主机名 String hostName = localHost.getHostName(); System.out.println(hostName); //获取ip String hostAddress = localHost.getHostAddress(); System.out.println(hostAddress); //获取指定域名或者ip的ip协议对象 InetAddress byName = InetAddress.getByName("www.baidu.com"); System.out.println(byName.getHostAddress()); System.out.println(byName.getHostName()); InetAddress localhost = InetAddress.getByName("10.41.12.50"); String hostAddress1 = localhost.getHostAddress(); System.out.println(hostAddress1); String hostName1 = localhost.getHostName(); System.out.println(hostName1); } }