Search in sources :

Example 91 with InetAddress

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

the class InitConnectionTask method bindSocket.

private void bindSocket(SocketChannel socketChannel) throws IOException {
    InetAddress inetAddress = getInetAddress();
    Socket socket = socketChannel.socket();
    if (connectionManager.useAnyOutboundPort()) {
        SocketAddress socketAddress = new InetSocketAddress(inetAddress, 0);
        socket.bind(socketAddress);
    } else {
        IOException ex = null;
        int retryCount = connectionManager.getOutboundPortCount() * 2;
        for (int i = 0; i < retryCount; i++) {
            int port = connectionManager.acquireOutboundPort();
            SocketAddress socketAddress = new InetSocketAddress(inetAddress, port);
            try {
                socket.bind(socketAddress);
                return;
            } catch (IOException e) {
                ex = e;
                logger.finest("Could not bind port[ " + port + "]: " + e.getMessage());
            }
        }
        throw ex;
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress) Socket(java.net.Socket)

Example 92 with InetAddress

use of java.net.InetAddress 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 93 with InetAddress

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

the class DefaultAddressPickerTest method testBindAddress.

private void testBindAddress(InetAddress address) throws Exception {
    addressPicker = new DefaultAddressPicker(config, properties, logger);
    addressPicker.pickAddress();
    int port = config.getNetworkConfig().getPort();
    assertEquals(new Address(address, port), addressPicker.getBindAddress());
    assertEquals(addressPicker.getBindAddress(), addressPicker.getPublicAddress());
}
Also used : Address(com.hazelcast.nio.Address) InetAddress(java.net.InetAddress) Inet6Address(java.net.Inet6Address)

Example 94 with InetAddress

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

the class DefaultAddressPickerTest method testBindAddress_withNonLoopbackAddressViaTCPMembers.

@Test
public void testBindAddress_withNonLoopbackAddressViaTCPMembers() throws Exception {
    InetAddress address = findIPv4NonLoopbackInterface();
    assumeNotNull(address);
    config.getNetworkConfig().getJoin().getTcpIpConfig().setEnabled(true).clear().addMember(address.getHostAddress());
    testBindAddress(address);
}
Also used : InetAddress(java.net.InetAddress) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 95 with InetAddress

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

the class DefaultAddressPickerTest method setup.

@Before
public void setup() throws UnknownHostException {
    properties = new HazelcastProperties(config);
    InetAddress publicAddress = null;
    try {
        loopback = InetAddress.getByName("127.0.0.1");
        publicAddress = InetAddress.getByName(PUBLIC_HOST);
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }
    assumeNotNull(loopback, publicAddress);
    localAddressValue = System.getProperty(HAZELCAST_LOCAL_ADDRESS_PROP);
    System.clearProperty(HAZELCAST_LOCAL_ADDRESS_PROP);
}
Also used : HazelcastProperties(com.hazelcast.spi.properties.HazelcastProperties) UnknownHostException(java.net.UnknownHostException) InetAddress(java.net.InetAddress) Before(org.junit.Before)

Aggregations

InetAddress (java.net.InetAddress)2225 UnknownHostException (java.net.UnknownHostException)423 IOException (java.io.IOException)295 Test (org.junit.Test)289 InetSocketAddress (java.net.InetSocketAddress)251 NetworkInterface (java.net.NetworkInterface)192 ArrayList (java.util.ArrayList)174 SocketException (java.net.SocketException)154 HashMap (java.util.HashMap)104 Inet6Address (java.net.Inet6Address)103 Inet4Address (java.net.Inet4Address)96 Socket (java.net.Socket)78 DatagramPacket (java.net.DatagramPacket)75 LinkAddress (android.net.LinkAddress)70 DatagramSocket (java.net.DatagramSocket)67 Token (org.apache.cassandra.dht.Token)67 Map (java.util.Map)59 RouteInfo (android.net.RouteInfo)56 LinkProperties (android.net.LinkProperties)52 ServerSocket (java.net.ServerSocket)52