InetAddress.getAllByName(String host): This method is available in the java.net.InetAddress class of Java.
Syntax:
InetAddress[] java.net.InetAddress.getAllByName(String host) throws UnknownHostException
This method takes one argument. This method returns an array of its IP addresses,based on the configured name service on the system.
The host name can either be a machine name, such as"www.example.com", or a textual representation of its IPaddress. If a literal IP address is supplied, only the validity of the address format is checked.
Parameters: One parameter is required for this method.
host: the name of the host, or null.
Returns: an array of all the IP addresses for a given host name.
Throws:
1. UnknownHostException - if no IP address for the host could be found, or if a scope_id was specified for a global IPv6 address.
2. SecurityException - if a security manager exists and its checkConnect method doesn't allow the operation.
Approach 1: When no exception
Java
package com.InetAddress;import java.net.InetAddress;import java.net.UnknownHostException;import java.util.Arrays;public class InetAddressgetAllByName {public static void main(String[] args)throws UnknownHostException {String host = "beingcodeexpert.blogspot.com";InetAddress[] inetAddress =InetAddress.getAllByName(host);System.out.println(Arrays.toString(inetAddress));}}
Output:
[beingcodeexpert.blogspot.com/142.250.193.225, beingcodeexpert.blogspot.com/2404:6800:4002:81f:0:0:0:2001]
Approach 2: UnknownHostException
Java
package com.InetAddress;import java.net.InetAddress;import java.net.UnknownHostException;import java.util.Arrays;public class InetAddressgetAllByName {public static void main(String[] args)throws UnknownHostException {String host = "hello";InetAddress[] inetAddress =InetAddress.getAllByName(host);System.out.println(Arrays.toString(inetAddress));}}
Output:
Exception in thread "main" java.net.UnknownHostException: No such host is known (hello) at java.base/java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method) at java.base/java.net.InetAddress$PlatformNameService.lookupAllHostAddr(InetAddress.java:932) at java.base/java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1517) at java.base/java.net.InetAddress$NameServiceAddresses.get(InetAddress.java:851) at java.base/java.net.InetAddress.getAllByName0(InetAddress.java:1507) at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1366) at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1300) at com.InetAddress.InetAddressgetAllByName.main(InetAddressgetAllByName.java:11)
Some other methods of InetAddress class
equals(Object): This method compares this object against the specified object.
getAddress(): This method returns the raw IP address of this InetAddress object.
InetAddress.getAllByName(String host): This method returns an array of its IP addresses, based on the configured name service on the system.
InetAddress.getByAddress(byte[]): This method returns an InetAddress object given the raw IP address.
InetAddress.getByAddress(String, byte[]): This method creates an InetAddress based on the provided hostname and IP address.
InetAddress.getByName(String): This method determines the IP address of a host, given the host's name.
getCanonicalHostName(): This method gets the fully qualified domain name for this IP address.
getHostAddress(): This method returns the IP address string in textual presentation.
getHostName(): This method gets the hostname for this IP address.
InetAddress.getLocalHost(): This method returns the address of the local host.
InetAddress.getLoopbackAddress(): This method returns the loopback address.
hashCode(): This method returns a hashcode for this IP address.
isAnyLocalAddress(): Utility routine to check if the InetAddress is a wildcard address.
isLinkLocalAddress(): Utility routine to check if the InetAddress is a link-local address.
isLoopbackAddress(): Utility routine to check if the InetAddress is a loopback address.
isMCGlobal(): Utility routine to check if the multicast address has global scope.
isMCLinkLocal(): Utility routine to check if the multicast address has link scope.
isMCNodeLocal(): Utility routine to check if the multicast address has node scope.
isMCOrgLocal(): Utility routine to check if the multicast address has organization scope.
isMCSiteLocal(): Utility routine to check if the multicast address has site scope.
isMulticastAddress(): Utility routine to check if the InetAddress is an IP multicast address.
isReachable(int): This method tests whether that address is reachable.
isReachable(NetworkInterface, int, int): This method tests whether that address is reachable.
isSiteLocalAddress(): Utility routine to check if the InetAddress is a site-local address.
toString(): This method converts this IP address to a String.
No comments:
Post a Comment