InetAddress
InetAddress 类没有构造函数,说明是由静态方法返回对象引用的,大胆猜测是单例模式
InetAddress继承关系
InetAddress的以下静态成员方法是返回对象引用
Method Modifier and Type Description 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 Type Description 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 {
InetAddress addr = InetAddress. getLocalHost ( ) ;
System. out. println ( addr. getHostAddress ( ) ) ;
System. out. println ( addr. getHostName ( ) ) ;
addr = InetAddress. getByName ( "www.163.com" ) ;
System. out. println ( addr. getHostAddress ( ) ) ;
System. out. println ( addr. getHostName ( ) ) ;
addr = InetAddress. getByName ( "124.163.208.141" ) ;
System. out. println ( addr. getHostAddress ( ) ) ;
System. out. println ( addr. getHostName ( ) ) ;
}
}