首页 > 编程语言 > 详细

JAVA-网络编程-InetAddress类

时间:2020-04-26 21:34:21      阅读:59      评论:0      收藏:0      [点我收藏+]

JAVA-网络编程-InetAddress类


InetAddress类

此类表示互联网协议 (IP) 地址。
InetAddress类没有构造方法,所以不能直接new实例化。
要获得InetAddress实例可以使用该类的静态方法

方法

返回值类型 方法 说明
static InetAddress getByAddress(byte[] addr) 在给定原始 IP 地址的情况下,返回 InetAddress 对象。
static InetAddress static InetAddress 根据提供的主机名和 IP 地址创建 InetAddress。
static InetAddress getByName(String host) 以ip地址返回实例 在给定主机名的情况下确定主机的 IP 地址。
static InetAddress getLocalHost() 返回本地主机。
String getCanonicalHostName() 获取此 IP 地址的完全限定域名。
String getHostAddress() 返回 IP 地址字符串(以文本表现形式)。
String getHostName() 获取此 IP 地址的主机名

代码

public static void main(String[] args) throws UnknownHostException {
	// 获取实例
	InetAddress inet2 = InetAddress.getByName("198.2.2.2");
	// inet1相当于InetAddress.getlocalHost();
	InetAddress inet1 = InetAddress.getLocalHost();
	// 获取属性
	System.out.println(inet1.getHostAddress());	// 192.168.42.143
	System.out.println(inet1.getHostName());	// DESKTOP-4JN4PSM
	System.out.println(inet1.getAddress());		// [B@7852e922
	System.out.println(inet1.getCanonicalHostName());	// DESKTOP-4JN4PSM
}

JAVA-网络编程-InetAddress类

原文:https://www.cnblogs.com/torain/p/12781852.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!