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;
}
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;
}
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;
}
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;
}
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();
}
Aggregations