package org.academiadecodigo.enuminatti.inetaddress;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * MIT License
 * (c) 2017 Ricardo Constantino
 */

public class Main {
    public static void main(String[] args) {

        String[] hosts = new String[]{
                "google.com",
                "academiadecodigo.org",
                "fundao.academiadecodigo.org",
                "carcavelos.academiadecodigo.org"
        };

        byte[][] ipAddresses = new byte[][]{
            new byte[] {8, 8, 8, 8},
                new byte[] {8, 8, 4, 4}
        };

        InetAddress[] resolvedHosts = new InetAddress[hosts.length + ipAddresses.length];

        // host name resolution

        for (int i = 0; i < hosts.length; i++) {
            try {
                resolvedHosts[i] = InetAddress.getByName(hosts[i]);
//                System.out.println(resolvedHosts[i]);
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
        }
        for (int i = hosts.length; i < hosts.length+ipAddresses.length; i++) {
            try {
                resolvedHosts[i] = InetAddress.getByAddress(ipAddresses[i - hosts.length]);
//                System.out.println(resolvedHosts[i]);
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
        }

        // reachability

        for (InetAddress host : resolvedHosts) {
            System.out.println();
            System.out.println("ip address: " + host.getHostAddress());
            System.out.println("host: " + host.getHostName());
            System.out.println("canonical host: " + host.getCanonicalHostName());
            try {
                System.out.println("is reachable: " + host.isReachable(null, 0, 5000));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
