/** * Get a local, IP4 Address, preferable a non-loopback address which is bound to an interface. * * @return * @throws UnknownHostException * @throws SocketException */ public static InetAddress getLocalAddress() throws UnknownHostException, SocketException { InetAddress addr = InetAddress.getLocalHost(); NetworkInterface nif = NetworkInterface.getByInetAddress(addr); if (addr.isLoopbackAddress() || addr instanceof Inet6Address || nif == null) { // Find local address that isn't a loopback address InetAddress lookedUpAddr = findLocalAddressViaNetworkInterface(); // If a local, multicast enabled address can be found, use it. Otherwise // we are using the local address, which might not be what you want addr = lookedUpAddr != null ? lookedUpAddr : InetAddress.getByName("127.0.0.1"); } return addr; }
/** * Get a local address which supports multicast. A loopback adress is returned, but only if not * another is available * * @return a multicast enabled address of null if none could be found * @throws UnknownHostException * @throws SocketException */ public static InetAddress getLocalAddressWithMulticast() throws UnknownHostException, SocketException { InetAddress addr = InetAddress.getLocalHost(); NetworkInterface nif = NetworkInterface.getByInetAddress(addr); if (addr.isLoopbackAddress() || addr instanceof Inet6Address || !isMulticastSupported(nif)) { // Find local address that isn't a loopback address InetAddress lookedUpAddr = findLocalAddressViaNetworkInterface(); // If a local, multicast enabled address can be found, use it. Otherwise // we are using the local address, which might not be what you want if (lookedUpAddr != null) { return lookedUpAddr; } addr = InetAddress.getByName("127.0.0.1"); } if (isMulticastSupported(addr)) { return addr; } else { throw new UnknownHostException("Cannot find address of local host which can be used for multicasting"); } }
/** * Get a local, IP4 Address, preferable a non-loopback address which is bound to an interface. * * @return * @throws UnknownHostException * @throws SocketException */ public static InetAddress getLocalAddress() throws UnknownHostException, SocketException { InetAddress addr = InetAddress.getLocalHost(); NetworkInterface nif = NetworkInterface.getByInetAddress(addr); if (addr.isLoopbackAddress() || addr instanceof Inet6Address || nif == null) { // Find local address that isn't a loopback address InetAddress lookedUpAddr = findLocalAddressViaNetworkInterface(); // If a local, multicast enabled address can be found, use it. Otherwise // we are using the local address, which might not be what you want addr = lookedUpAddr != null ? lookedUpAddr : InetAddress.getByName("127.0.0.1"); } return addr; }
/** * Get a local, IP4 Address, preferable a non-loopback address which is bound to an interface. * * @return * @throws UnknownHostException * @throws SocketException */ public static InetAddress getLocalAddress() throws UnknownHostException, SocketException { InetAddress addr = InetAddress.getLocalHost(); NetworkInterface nif = NetworkInterface.getByInetAddress(addr); if (addr.isLoopbackAddress() || addr instanceof Inet6Address || nif == null) { // Find local address that isn't a loopback address InetAddress lookedUpAddr = findLocalAddressViaNetworkInterface(); // If a local, multicast enabled address can be found, use it. Otherwise // we are using the local address, which might not be what you want addr = lookedUpAddr != null ? lookedUpAddr : InetAddress.getByName("127.0.0.1"); } return addr; }
/** * Get a local address which supports multicast. A loopback adress is returned, but only if not * another is available * * @return a multicast enabled address of null if none could be found * @throws UnknownHostException * @throws SocketException */ public static InetAddress getLocalAddressWithMulticast() throws UnknownHostException, SocketException { InetAddress addr = InetAddress.getLocalHost(); NetworkInterface nif = NetworkInterface.getByInetAddress(addr); if (addr.isLoopbackAddress() || addr instanceof Inet6Address || !isMulticastSupported(nif)) { // Find local address that isn't a loopback address InetAddress lookedUpAddr = findLocalAddressViaNetworkInterface(); // If a local, multicast enabled address can be found, use it. Otherwise // we are using the local address, which might not be what you want if (lookedUpAddr != null) { return lookedUpAddr; } addr = InetAddress.getByName("127.0.0.1"); } if (isMulticastSupported(addr)) { return addr; } else { throw new UnknownHostException("Cannot find address of local host which can be used for multicasting"); } }
/** * Get a local address which supports multicast. A loopback adress is returned, but only if not * another is available * * @return a multicast enabled address of null if none could be found * @throws UnknownHostException * @throws SocketException */ public static InetAddress getLocalAddressWithMulticast() throws UnknownHostException, SocketException { InetAddress addr = InetAddress.getLocalHost(); NetworkInterface nif = NetworkInterface.getByInetAddress(addr); if (addr.isLoopbackAddress() || addr instanceof Inet6Address || !isMulticastSupported(nif)) { // Find local address that isn't a loopback address InetAddress lookedUpAddr = findLocalAddressViaNetworkInterface(); // If a local, multicast enabled address can be found, use it. Otherwise // we are using the local address, which might not be what you want if (lookedUpAddr != null) { return lookedUpAddr; } addr = InetAddress.getByName("127.0.0.1"); } if (isMulticastSupported(addr)) { return addr; } else { throw new UnknownHostException("Cannot find address of local host which can be used for multicasting"); } }