- Given hostname in java, we would like to find out network details of host.
- JDK class InetAddress provide APIs to fetch network details of hosts.
- We will retrieve network details for:
- Loopback address or localhost
- Get network details for public hosts like facebook or youtube.
- Also, get network details of host if it is mapped to multiple IP addresses.
Get Network details like IPAddress, hostname for loopback:
Example 1: Input loopback hostname i.e. localhost We will use API InetAddress.getLoopbackAddress() to get loopback details. Output: We would get network details like IP Address, hostname as follows Ip Address:127.0.0.1, Host Name: localhost, Canonical Name: 127.0.0.1 Example 2: Get network details for localhost We will use InetAddress.getLocalHost() to get details like IP address, canonical name. Output for localhost is: Ip Address:192.168.43.201, Host Name: 5AYX2L07 |
Get network details for public hosts like facebook or youtube:
Example 3: Get network details for public hosts like facebook or youtube. We will use API InetAddress.getByName( "www.facebook.com" ) to fetch network details. Output: currently, we are getting following details (it may vary) Ip Address:157.240.7.35, Host Name: www.facebook.com, Canonical Name: edge-star-mini-shv-01-sin6.facebook.com Example 4: Get all network addresses if hostname is mapped to multiple IP address. Retrieve all network Address of hostname using API InetAddress.getAllByName( "www.fb.com" ) Output: we are getting output as follows: Ip Address:31.13.78.35, Host Name: www.fb.com |
Network details for hosts (loopback, local & standard hosts) in java
package org.learn.nw; import java.net.InetAddress; import java.net.UnknownHostException; public class HostNameIpAddress { public static void main(String[] args) throws UnknownHostException { //Loop back address and hostname InetAddress inetAddress = InetAddress.getLoopbackAddress(); System.out.println( "1. Loop back ip address and hostname" ); display(inetAddress); //Local ip address and hostname System.out.println( "\n2. Local ip address and hostname" ); inetAddress = InetAddress.getLocalHost(); display(inetAddress); //Get IP address of given hostname System.out.println( "\n3. Standard host ip address from hostname" ); inetAddress = InetAddress.getByName( "www.facebook.com" ); display(inetAddress); //Get list of IP address for given hostname. System.out.println( "\n4. All IP address corresponding to given hostname" ); InetAddress[] allInetAddress = InetAddress.getAllByName( "www.fb.com" ); for (InetAddress netAddress : allInetAddress) { display(netAddress); System.out.println(); } } private static void display(InetAddress inetAddress) { System.out.printf( "Ip Address:%s, Host Name: %s, Canonical Name: %s" , inetAddress.getHostAddress(), inetAddress.getHostName(), inetAddress.getCanonicalHostName()); } } |
Output: Network details for hosts in java (example)
1. Loop back ip address and hostname Ip Address:127.0.0.1, Host Name: localhost, Canonical Name: 127.0.0.1 2. Local ip address and hostname Ip Address:192.168.43.201, Host Name: 5AYX2L07, Canonical Name: 2YR1L72.xyz.com 3. Standard host ip address from hostname Ip Address:157.240.7.35, Host Name: www.facebook.com, Canonical Name: edge-star-mini-shv-01-sin6.facebook.com 4. All IP address corresponding to given hostname Ip Address:31.13.78.35, Host Name: www.fb.com, Canonical Name: edge-star-mini-shv-01-sit4.facebook.com |