Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package com.splout.db.common;
 
 /*
  * #%L
  * Splout SQL Server
  * %%
  * Copyright (C) 2012 Datasalt Systems S.L.
  * %%
  * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * #L%
 */
From: http://pastebin.com/5X073pUc
public class GetIPAddresses {
	private final static Log log = LogFactory.getLog(GetIPAddresses.class);

Returns all available IP addresses.

In error case or if no network connection is established, we return an empty list here.

Loopback addresses are excluded - so 127.0.0.1 will not be never returned.

The "primary" IP might not be the first one in the returned list.

Returns:
Returns all IP addresses (can be an empty list in error case or if network connection is missing).
Throws:
java.net.SocketException
Since:
0.1.0
	public static Collection<InetAddressgetAllLocalIPs() throws SocketException {
		Enumeration<NetworkInterfacenifs = NetworkInterface.getNetworkInterfaces();
		if(nifs == null)
			return listAdr;
		while(nifs.hasMoreElements()) {
			// We ignore subinterfaces - as not yet needed.
			while(adrs.hasMoreElements()) {
				InetAddress adr = adrs.nextElement();
				if(adr != null && !adr.isLoopbackAddress()
				    && (nif.isPointToPoint() || !adr.isLinkLocalAddress())) {
					.info("Available site local address: " + adr);
					listAdr.add(adr);
				}
			}
		}
		return listAdr;
	}
	public static void main(String[] argsthrows SocketException {
		GetIPAddresses.getAllLocalIPs();
	}
New to GrepCode? Check out our FAQ X