📜  Java中的 InetAddress 类

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

Java中的 InetAddress 类

IP 地址是具有有关如何到达特定主机的信息的地址,它是具有 2^32 地址空间的 32 位唯一地址编号。 InetAddress 类是 IP 地址的表示。它代表 32 位 IPv4 地址和 128 位 IPv6 地址。它是 Inet6Address 和 Inet4Address 类的超类。此类的实例由 IP 地址和通常的主机名组成,具体取决于在创建期间是否执行了主机名解析。

InetAddress 类的方法

MethodAction Performed
equals() Returns true if this IP address is the same as that of the object specified. Equals() method don’t consider hostnames while comparing and only consider IP address associated. 
getAddress()Returns the raw IP address of this InetAddress object as an array. The order in which bytes appear in an array is the same as in IP address i.e. getAddress[0] will contain the highest order byte. 
getByAddress()Create an InetAddress object. It takes the hostname and IP address as its parameter. The hostname can be the machine name as in “www.geeksforgeeks.org” or its textual IP address. 
getByName()Returns the IP Address of the host specified. If the host is a literal IP address, then only its validity is checked.
getAllByName()Returns an array of IP addresses for the given host
getLoopbackAddress()Returns the loopback address
getHostAddress()Returns IP address in textual form.
getHostName()Returns the hostname for this IP Address. If this object was created with a hostname then it is returned, otherwise, a reverse lookup is performed to return the system configured hostname. 
getLocalHost()Returns the IP address of the local host. 
getCanonicalHostName()Returns the fully qualified domain name for this object. If this object was created with a hostname then it is returned, otherwise, a reverse lookup is performed to return the system configured hostname. 
hashCode()Returns the hashcode associated with this address object. 
isAnyLocalAddress()Returns true if this address represents a local address.
isLinkLocalAddress()Returns true if this address is a link-local address.
isLoopbackAddress()Returns true if this address is a loopback address.
isMCGlobal()Returns true if this multicast address has global scope. 
isMCLinkLocal()Returns true if this multicast address has link scope. 
isMCNodeLocal()Returns true if this multicast address has node scope. 
isMCOrgLocal()Returns true if this multicast address has organization scope. 
 isMCSiteLocal()Returns true if this multicast address has site scope. 
isMulticastAddress()Returns true if this address is an IP multicast address. Multicast addresses have 1110 as their first 4 bits.
isReachable()Returns true if this address is reachable. ICMP echo requests are used if permission can be granted otherwise the host tries to make a TCP connection at port 7 of the destination. This method is used generally as a pre-condition in various programs, to avoid Host Unreachable exceptions in the future
 isReachable()Specify the network interface to be used while checking for reachability and the ttl parameter specifies the number of hops the echo packet makes before exiting the network.
isSiteLocalAddress()Returns true if this address is a site-local address.
toString() Converts the IP address to the string. It returns the result as hostname / IP address.

例子:

Java
// Java Program to Illustrate Methods of Inetaddress Class
 
// Importing required classes
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
        throws UnknownHostException
    {
 
        // Input URL
        String url = "www.geeksforgeeks.org";
        byte addr[] = { 127, 0, 0, 1 };
 
        InetAddress ip1 = Inet4Address.getByName(url);
        InetAddress ip2 = InetAddress.getByAddress(addr);
 
        // Following methods checks the property
        // of the thus created object
 
        // getAddress() method
        System.out.println(
            "Address : "
            + Arrays.toString(ip1.getAddress()));
 
        // getHostAddress() method
        System.out.println("Host Address : "
                           + ip1.getHostAddress());
 
        // isAnyLocalAddress() method
        System.out.println("isAnyLocalAddress : "
                           + ip1.isAnyLocalAddress());
 
        // isLinkLocalAddress() method
        System.out.println("isLinkLocalAddress : "
                           + ip1.isLinkLocalAddress());
 
        // isLoopbackAddress() method
        System.out.println("isLoopbackAddress : "
                           + ip1.isLoopbackAddress());
 
        // isMCGlobal() method
        System.out.println("isMCGlobal : "
                           + ip1.isMCGlobal());
 
        // isMCLinkLocal() method
        System.out.println("isMCLinkLocal : "
                           + ip1.isMCLinkLocal());
 
        // isMCNodeLocal() method
        System.out.println("isMCNodeLocal : "
                           + ip1.isMCNodeLocal());
 
        // isMCOrgLocal() method
        System.out.println("isMCOrgLocal : "
                           + ip1.isMCOrgLocal());
 
        // isMCSiteLocal() method
        System.out.println("isMCSiteLocal : "
                           + ip1.isMCSiteLocal());
 
        // isMulticastAddress() method
        System.out.println("isMulticastAddress : "
                           + ip1.isMulticastAddress());
 
        // isSiteLocalAddress() method
        System.out.println("isSiteLocalAddress : "
                           + ip1.isSiteLocalAddress());
 
        // hashCode() method
        System.out.println("hashCode : " + ip1.hashCode());
 
        // equals() method
        System.out.println("ip1==ip2 : " + ip1.equals(ip2));
    }
}


Java
// Java Program to Illustrate Methods of Inetaddress Class
 
// Importing required classes
import java.io.IOException;
import java.net.InetAddress;
import java.util.Arrays;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
        throws IOException
    {
 
        // Input sample URL
        String url = "www.geeksforgeeks.org";
        byte addr[] = { 127, 0, 0, 1 };
 
        // getByName() method
        InetAddress ip1 = InetAddress.getByName(url);
        System.out.println("getByName() : " + ip1);
 
        // getByAddress() method
        InetAddress ip2 = InetAddress.getByAddress(addr);
        System.out.println("getByAddress() : " + ip2);
 
        // getLocalHost() method
        InetAddress ip3 = InetAddress.getLocalHost();
        System.out.println("getLocalHost() : " + ip3);
 
        // getLoopbackAddress() method
        InetAddress ip4 = InetAddress.getLoopbackAddress();
        System.out.println("getLoopbackAddress() : " + ip4);
 
        // getAllByName() method
        // Returns all ip addresses associated with
        // 'google.com'
        InetAddress addrs[]
            = InetAddress.getAllByName("www.google.com");
        System.out.println("Google ip addresses : "
                           + Arrays.toString(addrs));
 
        // isReachable() method
        boolean isreach = ip1.isReachable(50);
        System.out.println("ip1 isReachable() : "
                           + isreach);
 
        // getHostname() method
        String hostname = ip1.getHostName();
        System.out.println("ip1 hostname :" + hostname);
 
        // getCanonicalHostname() method
        System.out.println("ip1 CanonicalHostname : "
                           + ip1.getCanonicalHostName());
 
        // toString() method
        System.out.println("ip1 toString() : "
                           + ip1.toString());
    }
}


Java
// Java program to Demonstrate Working of InetAddress Class
// by Finding IP address for a Domain Name
 
// Importing required classes
import java.net.*;
 
// Main class
// GetIPAddress
public class GFG {
 
    // Main driver method
    public static void main(String args[]) throws Exception
    {
 
        // Input sample URL
        String url = "www.google.com";
 
        // Try block to check for exceptions
        try {
 
            // Getting IP addresses related to the domain
            InetAddress ips[]
                = InetAddress.getAllByName(url);
 
            // Displaying IP addresses
            System.out.println("IP Address(es)");
 
            for (InetAddress addr : ips)
                System.out.println(addr.getHostAddress());
        }
 
        // Catch block to handle exceptions
        catch (Exception ex) {
 
            // Display message if exception occurs
            System.out.println("host not found");
        }
    }
}


输出:

示例 2:

Java

// Java Program to Illustrate Methods of Inetaddress Class
 
// Importing required classes
import java.io.IOException;
import java.net.InetAddress;
import java.util.Arrays;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
        throws IOException
    {
 
        // Input sample URL
        String url = "www.geeksforgeeks.org";
        byte addr[] = { 127, 0, 0, 1 };
 
        // getByName() method
        InetAddress ip1 = InetAddress.getByName(url);
        System.out.println("getByName() : " + ip1);
 
        // getByAddress() method
        InetAddress ip2 = InetAddress.getByAddress(addr);
        System.out.println("getByAddress() : " + ip2);
 
        // getLocalHost() method
        InetAddress ip3 = InetAddress.getLocalHost();
        System.out.println("getLocalHost() : " + ip3);
 
        // getLoopbackAddress() method
        InetAddress ip4 = InetAddress.getLoopbackAddress();
        System.out.println("getLoopbackAddress() : " + ip4);
 
        // getAllByName() method
        // Returns all ip addresses associated with
        // 'google.com'
        InetAddress addrs[]
            = InetAddress.getAllByName("www.google.com");
        System.out.println("Google ip addresses : "
                           + Arrays.toString(addrs));
 
        // isReachable() method
        boolean isreach = ip1.isReachable(50);
        System.out.println("ip1 isReachable() : "
                           + isreach);
 
        // getHostname() method
        String hostname = ip1.getHostName();
        System.out.println("ip1 hostname :" + hostname);
 
        // getCanonicalHostname() method
        System.out.println("ip1 CanonicalHostname : "
                           + ip1.getCanonicalHostName());
 
        // toString() method
        System.out.println("ip1 toString() : "
                           + ip1.toString());
    }
}

输出:

实现:以下程序使用 InetAddress 类来获取给定域名的 IP 地址。当程序在连接到 Internet 的系统上运行时,它会给出给定域的 IP 地址。

例子:

Java

// Java program to Demonstrate Working of InetAddress Class
// by Finding IP address for a Domain Name
 
// Importing required classes
import java.net.*;
 
// Main class
// GetIPAddress
public class GFG {
 
    // Main driver method
    public static void main(String args[]) throws Exception
    {
 
        // Input sample URL
        String url = "www.google.com";
 
        // Try block to check for exceptions
        try {
 
            // Getting IP addresses related to the domain
            InetAddress ips[]
                = InetAddress.getAllByName(url);
 
            // Displaying IP addresses
            System.out.println("IP Address(es)");
 
            for (InetAddress addr : ips)
                System.out.println(addr.getHostAddress());
        }
 
        // Catch block to handle exceptions
        catch (Exception ex) {
 
            // Display message if exception occurs
            System.out.println("host not found");
        }
    }
}

输出: