当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java java.net.InetAddress用法及代码示例


公开课InetAddress延伸对象实现可序列化:

java.net.InetAddress 类提供了获取任何主机名的 IP 地址的方法。 IP地址由32位或128位无符号数表示。 InetAddress 可以处理 IPv4 和 IPv6 地址。

地址有 2 种类型:

  1. 单播 —单个接口的标识符。
  2. 组播 —一组接口的标识符。

InetAddress - 工厂方法:

InetAddress类用于封装数字 IP 地址和该地址的域名。 InetAddress 类没有可见的构造函数。 InetAddress 类无法直接创建对象,因此工厂方法用于该目的。工厂方法是类中返回该类的对象的静态方法。

InetAddress 类中有 5 个工厂方法可用 -

方法 说明
公共静态InetAddress getLocalHost()抛出UnknownHostException 此方法返回包含本地主机名和地址的 InetAddress 实例。
公共静态InetAddress getByName(字符串主机)抛出UnknownHostException 此方法返回 InetAddress 的实例,其中包含由以下表示的主机的 IP 和主机名主持人争论。
公共静态 InetAddress[] getAllByName( String 主机名 )抛出UnknownHostException 此方法返回包含 IP 地址的 InetAddress 类实例的数组。
公共静态 InetAddress getByAddress( 字节 IPAddress[] )抛出UnknownHostException 此方法返回从原始 IP 地址创建的 InetAddress 对象。
公共静态 InetAddress getByAddress( String 主机名, byte IPAddress[] )抛出UnknownHostException 此方法根据提供的主机名和 IP 地址创建并返回 InetAddress。

下面是InetAddress类的Java实现,演示工厂方法的使用 -

Java


import java.io.*;
import java.net.*;
import java.util.*;
class GFG {
    public static void main(String[] args)
        throws UnknownHostException
    {
        // To get and print InetAddress of Local Host
        InetAddress address1 = InetAddress.getLocalHost();
        System.out.println("InetAddress of Local Host : "
                           + address1);
        // To get and print InetAddress of Named Host
        InetAddress address2
            = InetAddress.getByName("45.22.30.39");
        System.out.println("InetAddress of Named Host : "
                           + address2);
        // To get and print ALL InetAddresses of Named Host
        InetAddress address3[]
            = InetAddress.getAllByName("172.19.25.29");
        for (int i = 0; i < address3.length; i++) {
            System.out.println(
                "ALL InetAddresses of Named Host : "
                + address3[i]);
        }
        // To get and print InetAddresses of
        // Host with specified IP Address
        byte IPAddress[] = { 125, 0, 0, 1 };
        InetAddress address4
            = InetAddress.getByAddress(IPAddress);
        System.out.println(
            "InetAddresses of Host with specified IP Address : "
            + address4);
        // To get and print InetAddresses of Host
        // with specified IP Address and hostname
        byte[] IPAddress2
            = { 105, 22, (byte)223, (byte)186 };
        InetAddress address5 = InetAddress.getByAddress(
            "gfg.com", IPAddress2);
        System.out.println(
            "InetAddresses of Host with specified IP Address and hostname : "
            + address5);
    }
}
输出
InetAddress of Local Host : localhost/127.0.0.1
InetAddress of Named Host : /45.22.30.39
ALL InetAddresses of Named Host : /172.19.25.29
InetAddresses of Host with specified IP Address : /125.0.0.1
InetAddresses of Host with specified IP Address and hostname : gfg.com/105.22.223.186

InetAddress — 实例方法:

InetAddress 类有大量可以使用对象调用的实例方法。实例方法是 -

方法 说明
equals(Object obj) 此函数将此对象与指定对象进行比较。
getAddress() 此方法返回此 InetAddress 对象的原始 IP 地址(以字节为单位)。
getCanonicalHostName() 此方法返回此 IP 地址的完全限定域名。
getHostAddress() 该方法获取字符串形式的IP地址。
getHostName() 此方法返回此 IP 地址的主机名。
hashCode() 此方法获取此 IP 地址的哈希码。
isAnyLocalAddress() 此方法实用程序检查InetAddress是否是不可预测的地址。
isLinkLocalAddress() 此方法实用程序检查该地址是否未链接到本地单播地址。
isLoopbackAddress() 该方法用于检查InetAddress是否代表环回地址。
isMCGlobal() 该方法实用程序检查该地址是否具有全局范围的多播地址。
isMCLinkLocal() 该方法实用程序检查该地址是否具有link-local范围的多播地址。
isMCNodeLocal() 该方法实用程序检查多播地址是否具有节点范围。
isMCOrgLocal() 该方法实用程序检查多播地址是否具有组织范围。
isMCSiteLocal() 此方法实用程序检查多播地址是否具有站点范围。
isMulticastAddress() 此方法检查站点是否有多个服务器。
isReachable(int 超时) 此方法测试该地址是否可达。
isReachable(网络接口 netif, int ttl, int 超时) 此方法测试该地址是否可达。
isSiteLocalAddress() 该方法实用程序检查InetAddress是否是site-local地址。
toString() 该方法转换并返回字符串形式的IP地址。

下面是 InetAddress 类的 Java 实现,演示实例方法的使用 -

Java


import java.io.*;
import java.net.*;
import java.util.*;
class GFG {
    public static void main(String[] args)
        throws UnknownHostException
    {
        InetAddress address1
            = InetAddress.getByName("45.22.30.39");
        InetAddress address2
            = InetAddress.getByName("45.22.30.39");
        InetAddress address3
            = InetAddress.getByName("172.19.25.29");
        // true, as clearly seen above
        System.out.println(
            "Is Address-1 equals to Address-2? : "
            + address1.equals(address2));
        // false
        System.out.println(
            "Is Address-1 equals to Address-3? : "
            + address1.equals(address3));
        // returns IP address
        System.out.println("IP Address : "
                           + address1.getHostAddress());
        // returns host name,
        // which is same as IP
        // address in this case
        System.out.println(
            "Host Name for this IP Address : "
            + address1.getHostName());
        // returns address in bytes
        System.out.println("IP Address in bytes : "
                           + address1.getAddress());
        // false, as the given site
        //  has only one server
        System.out.println("Is this Address Multicast? : "
                           + address1.isMulticastAddress());
        System.out.println("Address in string form : "
                           + address1.toString());
        // returns fully qualified
        // domain name for this IP address.
        System.out.println(
            "Fully qualified domain name for this IP address : "
            + address1.getCanonicalHostName());
        // hashcode for this IP address.
        System.out.println("Hashcode for this IP address : "
                           + address1.hashCode());
        // to check if the InetAddress is
        // an unpredictable address..
        System.out.println(
            "Is the InetAddress an unpredictable address? : "
            + address1.isAnyLocalAddress());
    }
}
输出
Is Address-1 equals to Address-2? : true
Is Address-1 equals to Address-3? : false
IP Address : 45.22.30.39
Host Name for this IP Address : 45.22.30.39
IP Address in bytes : [B@579bb367
Is this Address Multicast? : false
Address in string form : 45.22.30.39/45.22.30.39
Fully qualified domain name for this IP address : 45.22.30.39
Hashcode for this IP address : 756424231
Is the InetAddress an unpredictable address? : false


相关用法


注:本文由纯净天空筛选整理自vanshitatwr620大神的英文原创作品 java.net.InetAddress Class in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。