📜  Java的.net.InetAddress类在Java中

📅  最后修改于: 2022-05-13 01:55:37.421000             🧑  作者: Mango

Java的.net.InetAddress类在Java中

公共类 InetAddress extends Object实现了可序列化:

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

有两种类型的地址:

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

InetAddress – 工厂方法:

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



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

Method Description
static InetAddress getLocalHost() throws UnknownHostExceptionThis method returns the instance of InetAddress containing the local hostname and address.
public static InetAddress getByName( String host ) throws UnknownHostExceptionThis method returns the instance of InetAddress containing LocalHost IP and name.
static InetAddress[] getAllByName( String hostName ) throws UnknownHostExceptionThis method returns the array of the instance of InetAddress class which contains IP addresses.
static InetAddress getByAddress( byte IPAddress[] ) throws UnknownHostExceptionThis method returns an InetAddress object created from the raw IP address.
static InetAddress getByAddress( String hostName, byte IPAddress[] ) throws UnknownHostExceptionThis method creates and returns an InetAddress based on the provided hostname and IP address.

下面是 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);
    }
}


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());
    }
}


输出
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 类有很多可以使用对象调用的实例方法。实例方法是 -

Method                           Description
equals(Object obj)This function compares this object against the specified object.
getAddress()This method returns the raw IP address of this InetAddress object, in bytes.
getCanonicalHostName()This method returns the fully qualified domain name for this IP address.
 getHostAddress()This method gets the IP address in string form.
 getHostName()This method returns the host name for this IP address.
 hashCode()This method gets a hashcode for this IP address.
isAnyLocalAddress()This method utility routine to check if the InetAddress is an unpredictable address.
 isLinkLocalAddress()This method utility routine to check if the address is not linked to local unicast address.
isLoopbackAddress()This method used to check if the InetAddress represents a loopback address.
 isMCGlobal()This method utility routine check if this address has a multicast address of global scope.
 isMCLinkLocal()This method utility routine check if this address has a multicast address of link-local scope.
 isMCNodeLocal()This method utility routine check if the multicast address has node scope.
 isMCOrgLocal()This method utility routine check if the multicast address has an organization scope.
 isMCSiteLocal()This method utility routine check if the multicast address has site scope.
 isMulticastAddress()This method checks whether the site has multiple servers.
 isReachable(int timeout)This method tests whether that address is reachable.
 isReachable(NetworkInterface netif, int ttl, int timeout)This method tests whether that address is reachable.
 isSiteLocalAddress()This method utility routine check if the InetAddress is a site-local address.
 toString()This method converts and returns an IP address in string form.

下面是 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