Search in sources :

Example 51 with NetworkInterface

use of java.net.NetworkInterface in project hazelcast by hazelcast.

the class AddressUtil method findRealInet6Address.

private static Inet6Address findRealInet6Address(Inet6Address inet6Address) throws SocketException {
    Inet6Address resultInetAddress = null;
    Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
    while (interfaces.hasMoreElements()) {
        NetworkInterface ni = interfaces.nextElement();
        Enumeration<InetAddress> addresses = ni.getInetAddresses();
        while (addresses.hasMoreElements()) {
            InetAddress address = addresses.nextElement();
            if (!isInet6Compatible(address, inet6Address)) {
                continue;
            }
            if (resultInetAddress != null) {
                throw new IllegalArgumentException("This address " + inet6Address + " is bound to more than one network interface!");
            }
            resultInetAddress = (Inet6Address) address;
        }
    }
    return resultInetAddress;
}
Also used : NetworkInterface(java.net.NetworkInterface) Inet6Address(java.net.Inet6Address) InetAddress(java.net.InetAddress)

Example 52 with NetworkInterface

use of java.net.NetworkInterface in project hazelcast by hazelcast.

the class DefaultAddressPickerTest method findIPv4NonLoopbackInterface.

private static InetAddress findIPv4NonLoopbackInterface() {
    try {
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        while (interfaces.hasMoreElements()) {
            NetworkInterface ni = interfaces.nextElement();
            Enumeration<InetAddress> addresses = ni.getInetAddresses();
            while (addresses.hasMoreElements()) {
                InetAddress address = addresses.nextElement();
                if (address.isLoopbackAddress()) {
                    continue;
                }
                if (address instanceof Inet6Address) {
                    continue;
                }
                return address;
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : SocketException(java.net.SocketException) NetworkInterface(java.net.NetworkInterface) Inet6Address(java.net.Inet6Address) InetAddress(java.net.InetAddress)

Example 53 with NetworkInterface

use of java.net.NetworkInterface in project hazelcast by hazelcast.

the class MulticastLoopbackModeTest method hasConfiguredNetworkInterface.

/**
     * Replies if a network interface was properly configured.
     *
     * @return <code>true</code> if there is at least one configured interface;
     * <code>false</code> otherwise.
     */
protected static boolean hasConfiguredNetworkInterface() {
    try {
        Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
        while (e.hasMoreElements()) {
            NetworkInterface i = e.nextElement();
            Enumeration<InetAddress> as = i.getInetAddresses();
            while (as.hasMoreElements()) {
                InetAddress a = as.nextElement();
                if (a instanceof Inet4Address && !a.isLoopbackAddress() && !a.isMulticastAddress()) {
                    return true;
                }
            }
        }
    } catch (Exception ignored) {
    // silently cast the exceptions
    }
    return false;
}
Also used : Inet4Address(java.net.Inet4Address) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

Example 54 with NetworkInterface

use of java.net.NetworkInterface in project JAirPort by froks.

the class Configuration method getHardwareAddress.

public static byte[] getHardwareAddress() {
    if (hwAddr == null || hwAddr.length == 0) {
        // MAC couldn't be determined
        try {
            InetAddress local = InetAddress.getLocalHost();
            NetworkInterface ni = NetworkInterface.getByInetAddress(local);
            if (ni != null) {
                hwAddr = ni.getHardwareAddress();
                return hwAddr;
            }
        } catch (Exception e) {
        // ignore
        }
        Random rand = new Random();
        byte[] mac = new byte[8];
        rand.nextBytes(mac);
        mac[0] = 0x00;
        hwAddr = mac;
    }
    return hwAddr;
}
Also used : Random(java.util.Random) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress) UnknownHostException(java.net.UnknownHostException)

Example 55 with NetworkInterface

use of java.net.NetworkInterface in project j2objc by google.

the class InetAddressTest method test_isReachable.

public void test_isReachable() throws Exception {
    // http://code.google.com/p/android/issues/detail?id=20203
    String s = "aced0005737200146a6176612e6e65742e496e6574416464726573732d9b57af" + "9fe3ebdb0200034900076164647265737349000666616d696c794c0008686f737" + "44e616d657400124c6a6176612f6c616e672f537472696e673b78704a7d9d6300" + "00000274000e7777772e676f6f676c652e636f6d";
    InetAddress inetAddress = InetAddress.getByName("www.google.com");
    new SerializationTester<InetAddress>(inetAddress, s) {

        @Override
        protected void verify(InetAddress deserialized) throws Exception {
            deserialized.isReachable(500);
            for (NetworkInterface nif : Collections.list(NetworkInterface.getNetworkInterfaces())) {
                deserialized.isReachable(nif, 20, 500);
            }
        }

        @Override
        protected boolean equals(InetAddress a, InetAddress b) {
            return a.getHostName().equals(b.getHostName());
        }
    }.test();
}
Also used : NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress) UnknownHostException(java.net.UnknownHostException)

Aggregations

NetworkInterface (java.net.NetworkInterface)225 InetAddress (java.net.InetAddress)178 SocketException (java.net.SocketException)112 ArrayList (java.util.ArrayList)42 Inet4Address (java.net.Inet4Address)39 UnknownHostException (java.net.UnknownHostException)36 InterfaceAddress (java.net.InterfaceAddress)32 Inet6Address (java.net.Inet6Address)29 IOException (java.io.IOException)25 Enumeration (java.util.Enumeration)9 InetSocketAddress (java.net.InetSocketAddress)7 HashSet (java.util.HashSet)7 LinkedHashSet (java.util.LinkedHashSet)7 Command (com.android.server.NativeDaemonConnector.Command)6 Test (org.junit.Test)6 DatagramSocket (java.net.DatagramSocket)5 File (java.io.File)4 MulticastSocket (java.net.MulticastSocket)4 List (java.util.List)4 SharedPreferences (android.content.SharedPreferences)3