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

net.yadaframework.raw.YadaNetworkUtil Maven / Gradle / Ivy

There is a newer version: 0.7.7.R4
Show newest version
package net.yadaframework.raw;
import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class YadaNetworkUtil {
	@SuppressWarnings("unused")
	private final transient Logger log = LoggerFactory.getLogger(getClass());
	
	/**
	 * Find all ipv4 and ipv6 addresses on the local host that can be used for sending packets to the internet
	 * @return
	 * @throws IOException
	 */
	public List findPublicAddresses() throws IOException {
		List result = new ArrayList();
		Enumeration networkInterfaces = NetworkInterface.getNetworkInterfaces();
		while (networkInterfaces.hasMoreElements()) {
			NetworkInterface networkInterface = networkInterfaces.nextElement();
			if (!networkInterface.isLoopback() && networkInterface.isUp()) {
				Enumeration inetAddresses = networkInterface.getInetAddresses();
				while (inetAddresses.hasMoreElements()) {
					InetAddress inetAddress = inetAddresses.nextElement();
					if (!inetAddress.isLinkLocalAddress()) {
						result.add(inetAddress);
					}
				}
			}
		}
		return result;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy