All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.lumongo.util.ServerNameHelper Maven / Gradle / Ivy

There is a newer version: 0.52
Show newest version
package org.lumongo.util;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;

public class ServerNameHelper {
	public static String getLocalServer() throws UnknownHostException, SocketException {
		Enumeration nicList;
		NetworkInterface nic;
		Enumeration nicAddrList;
		InetAddress nicAddr;
		
		nicList = NetworkInterface.getNetworkInterfaces();
		while (nicList.hasMoreElements()) {
			nic = nicList.nextElement();
			if (!nic.isLoopback() && nic.isUp()) {
				nicAddrList = nic.getInetAddresses();
				while (nicAddrList.hasMoreElements()) {
					nicAddr = nicAddrList.nextElement();
					if (nicAddr instanceof Inet4Address) {
						return nicAddr.getCanonicalHostName();
					}
				}
			}
		}
		
		return null;
		
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy