Java——InetAddress类
发布时间:2020-12-15 05:34:29 所属栏目:Java 来源:网络整理
导读:一、 InetAddress : 互联网协议 (IP) 地址;java.net 包; 二、获取对象方法: //没有构造方法; ( 1 ) static InetAddress?getLocalHost();获取本机IP 地址; //InetAddress lh = InetAddress.getLocalHost(); ( 2 ) static InetAddress?getByName(St
一、InetAddress:互联网协议 (IP) 地址;java.net包; 二、获取对象方法: //没有构造方法; (1)static InetAddress?getLocalHost();获取本机IP地址;//InetAddress lh = InetAddress.getLocalHost(); (2)static InetAddress?getByName(String host);根据主机名获取IP地址; (3)static InetAddress[]?getAllByName(String host);根据主机名获取多个IP地址; public class Test { public static void main(String[] args) throws UnknownHostException { InetAddress localHost = InetAddress.getLocalHost(); System.out.println(localHost); InetAddress byName = InetAddress.getByName("DESKTOP"); System.out.println(byName); InetAddress[] allByName = InetAddress.getAllByName("DESKTOP"); System.out.println(Arrays.toString(allByName)); } } 三、方法: (1)String?getHostName();获取此IP的主机名;//lh.getHostName(); (2)String?getHostAddress();获取IP的字符串类型; public class Test { public static void main(String[] args) throws UnknownHostException { InetAddress localHost = InetAddress.getLocalHost(); System.out.println(localHost.getHostName()); //DESKTOP System.out.println(localHost.getHostAddress()); //192.168.0.0 } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |