164.IP

InetAddress 12

InetAddress 类没有构造函数,说明是由静态方法返回对象引用的,大胆猜测是单例模式 3

InetAddress继承关系
InetAddress的以下静态成员方法是返回对象引用
Method Modifier and TypeDescription
static InetAddress[] getAllByName(String host)Given the name of a host, returns an array of its IP addresses, based on the configured name service on the system.
static InetAddress[] getAllByName(String host)Given the name of a host, returns an array of its IP addresses, based on the
static InetAddress getByAddress(byte[] addr)Returns an InetAddress object given the raw IP address .
static InetAddress getByAddress(String host, byte[] addr)Creates an InetAddress based on the provided host name and IP address.
InetAddress其它成员方法
Method Modifier and TypeDescription
boolean equals(Object obj)Compares this object against the specified object
String getCanonicalHostName()Gets the fully qualified domain name for this IP address.
String getHostAddress()Returns the IP address string in textual presentation.
String getHostName()Gets the host name for this IP address.
static InetAddress getLocalHost()Returns the address of the local host.
static InetAddress getLoopbackAddress()Returns the loopback address.
int hashCode()Returns a hashcode for this IP address.
boolean isAnyLocalAddress()Utility routine to check if the InetAddress is a wildcard address.
boolean isLinkLocalAddress()Utility routine to check if the InetAddress is an link local address.
boolean isLoopbackAddress()Utility routine to check if the InetAddress is a loopback address.
boolean isMCGlobal()Utility routine to check if the multicast address has global scope.
boolean isMCLinkLocal()Utility routine to check if the multicast address has link scope.
boolean isMCNodeLocal()Utility routine to check if the multicast address has node scope.
boolean isMCOrgLocal()Utility routine to check if the multicast address has organization scope.
boolean isMCSiteLocal()Utility routine to check if the multicast address has site scope.
boolean isMulticastAddress()Utility routine to check if the InetAddress is an IP multicast address.
boolean isReachable(int timeout)Test whether that address is reachable.
boolean isReachable(NetworkInterface netif, int ttl, int timeout)Test whether that address is reachable.
boolean isSiteLocalAddress()Utility routine to check if the InetAddress is a site local address.
String toString()Converts this IP address to a String.
代码演示
创建InetAddress对象
import java.net.*;
public class ipTest {
	public static void main(String[] args) throws UnknownHostException {
		//使用getLocalHost创建InetAddress对象
		InetAddress addr = InetAddress.getLocalHost(); //返回本地主机的地址
		System.out.println(addr.getHostAddress()); //返回IP地址字符串
		System.out.println(addr.getHostName()); //返回IP地址的主机名
		
		//根据域名得到InetAddress对象
		addr = InetAddress.getByName("www.163.com");
		System.out.println(addr.getHostAddress()); //163的ip地址
		System.out.println(addr.getHostName()); //163域名
		
		//根据ip得到InetAddress对象
		addr = InetAddress.getByName("124.163.208.141");
		System.out.println(addr.getHostAddress()); //163的ip地址
		System.out.println(addr.getHostName()); //逻辑上应该返回163域名,域名不存在或者DNS服务器不允许解析就只会返回ip地址
	}
}

  1. 在java.net包中 ↩︎

  2. This class represents an Internet Protocol (IP) address. ↩︎

  3. ↩︎

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值