Java Host Name Get getCanonicalHostName()

Here you can find the source of getCanonicalHostName()

Description

get Canonical Host Name

License

Open Source License

Declaration

public static String getCanonicalHostName() 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.net.InetAddress;

import java.net.NetworkInterface;
import java.net.SocketException;

import java.util.Enumeration;

public class Main {
    public static String getCanonicalHostName() {

        String result = new String();

        try {/*  www  . j av a  2 s  .c om*/

            Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();

            String eth0 = "";
            String wlan0 = "";

            while (en.hasMoreElements()) {

                NetworkInterface intf = en.nextElement();
                Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();

                if (intf.getName().equals("wlan0")) {

                    while (enumIpAddr.hasMoreElements()) {

                        InetAddress inetAddress = enumIpAddr.nextElement();
                        wlan0 = inetAddress.getCanonicalHostName();

                    }

                }

                if (intf.getName().equals("eth0")) {

                    while (enumIpAddr.hasMoreElements()) {

                        InetAddress inetAddress = enumIpAddr.nextElement();
                        eth0 = inetAddress.getCanonicalHostName();

                    }

                }

            }

            if (!eth0.equals("")) {

                result = eth0;

            } else {

                result = wlan0;
            }

        } catch (SocketException e) {
            e.printStackTrace();
        }

        return result.toLowerCase();
    }
}

Related

  1. getAllMyHostAdresses()
  2. getByName(String host)
  3. getCanonicalHostName()
  4. getCanonicalHostName()
  5. getCanonicalHostName()
  6. getCanonicalHostname()
  7. getCasServerHostName()