Here you can find the source of getInterfaceAddress(final String ifaceName, final boolean ipV4)
Parameter | Description |
---|---|
ifaceName | The interface name |
ipV4 | true if searching ipV4 address, false if searching ipV6. |
public static InetAddress getInterfaceAddress(final String ifaceName, final boolean ipV4)
//package com.java2s; /*/*from w w w . j a va 2s . c o m*/ * * JMeta - Meta's java implementation * * Copyright (C) 2013-2015 Pablo Joubert * Copyright (C) 2013-2015 Thomas Lavocat * Copyright (C) 2013-2015 Nicolas Michon * * This file is part of JMeta. * * JMeta 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. * * JMeta 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 Affero 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/>. * */ import java.net.Inet4Address; import java.net.Inet6Address; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.util.Collections; public class Main { /** * Get the first address found for the given interface. The returned address will be stored in the DHT for * the local-only mode. * * @param ifaceName The interface name * @param ipV4 true if searching ipV4 address, false if searching ipV6. * @return The IntetAddress or null if not found. */ public static InetAddress getInterfaceAddress(final String ifaceName, final boolean ipV4) { try { NetworkInterface iface = NetworkInterface.getByName(ifaceName); for (InetAddress ifAddr : Collections.list(iface.getInetAddresses())) { if (ifAddr instanceof Inet4Address && ipV4) { return ifAddr; } else if (ifAddr instanceof Inet6Address && !ipV4) { return ifAddr; } } return null; } catch (SocketException ex) { return null; } } }