Here you can find the source of getCanonicalHostName()
public static String getCanonicalHostName()
//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(); } }