Search in sources :

Example 41 with InetAddress

use of java.net.InetAddress in project otter by alibaba.

the class DefaultCommunicationClientImpl method buildParams.

// ===================== helper method ==================
private CommunicationParam buildParams(String addr) {
    CommunicationParam params = new CommunicationParam();
    String[] strs = StringUtils.split(addr, ":");
    if (strs == null || strs.length != 2) {
        throw new IllegalArgumentException("addr example: 127.0.0.1:1099");
    }
    InetAddress address = null;
    try {
        address = InetAddress.getByName(strs[0]);
    } catch (UnknownHostException e) {
        throw new CommunicationException("addr_error", "addr[" + addr + "] is unknow!");
    }
    params.setIp(address.getHostAddress());
    params.setPort(Integer.valueOf(strs[1]));
    return params;
}
Also used : CommunicationParam(com.alibaba.otter.shared.communication.core.model.CommunicationParam) UnknownHostException(java.net.UnknownHostException) CommunicationException(com.alibaba.otter.shared.communication.core.exception.CommunicationException) InetAddress(java.net.InetAddress)

Example 42 with InetAddress

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

the class DefaultAddressPicker method pickMatchingAddress.

private AddressDefinition pickMatchingAddress(Collection<InterfaceDefinition> interfaces) throws SocketException {
    Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
    boolean preferIPv4Stack = preferIPv4Stack();
    while (networkInterfaces.hasMoreElements()) {
        NetworkInterface ni = networkInterfaces.nextElement();
        Enumeration<InetAddress> e = ni.getInetAddresses();
        while (e.hasMoreElements()) {
            InetAddress inetAddress = e.nextElement();
            if (preferIPv4Stack && inetAddress instanceof Inet6Address) {
                continue;
            }
            if (interfaces != null && !interfaces.isEmpty()) {
                AddressDefinition address = match(inetAddress, interfaces);
                if (address != null) {
                    return address;
                }
            } else if (!inetAddress.isLoopbackAddress()) {
                return new AddressDefinition(inetAddress);
            }
        }
    }
    return null;
}
Also used : NetworkInterface(java.net.NetworkInterface) Inet6Address(java.net.Inet6Address) InetAddress(java.net.InetAddress) AddressUtil.fixScopeIdAndGetInetAddress(com.hazelcast.util.AddressUtil.fixScopeIdAndGetInetAddress)

Example 43 with InetAddress

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

the class InetAddressFactory method findCommunicationAddresses.

private void findCommunicationAddresses() throws UnknownHostException {
    communicationAddresses = new ArrayList<InetAddress>();
    if (inetAddresses.getLoopback().isEmpty()) {
        if (inetAddresses.getRemote().isEmpty()) {
            InetAddress fallback = InetAddress.getByName(null);
            logger.debug("No loopback addresses, using fallback {}", fallback);
            communicationAddresses.add(fallback);
        } else {
            logger.debug("No loopback addresses, using remote addresses instead.");
            communicationAddresses.addAll(inetAddresses.getRemote());
        }
    } else {
        communicationAddresses.addAll(inetAddresses.getLoopback());
    }
}
Also used : InetAddress(java.net.InetAddress)

Example 44 with InetAddress

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

the class MultiChoiceAddressSerializer method write.

@Override
public void write(Encoder encoder, MultiChoiceAddress address) throws IOException {
    UUID canonicalAddress = address.getCanonicalAddress();
    encoder.writeLong(canonicalAddress.getMostSignificantBits());
    encoder.writeLong(canonicalAddress.getLeastSignificantBits());
    encoder.writeInt(address.getPort());
    encoder.writeSmallInt(address.getCandidates().size());
    for (InetAddress inetAddress : address.getCandidates()) {
        encoder.writeBinary(inetAddress.getAddress());
    }
}
Also used : UUID(java.util.UUID) InetAddress(java.net.InetAddress)

Example 45 with InetAddress

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

the class SocketChannelImpl method socket.

/*
     * Getting the internal Socket If we have not the socket, we create a new
     * one.
     */
@Override
public synchronized Socket socket() {
    if (socket == null) {
        try {
            InetAddress addr = null;
            int port = 0;
            if (connectAddress != null) {
                addr = connectAddress.getAddress();
                port = connectAddress.getPort();
            }
            socket = new SocketAdapter(new PlainSocketImpl(fd, localPort, addr, port), this);
        } catch (SocketException e) {
            return null;
        }
    }
    return socket;
}
Also used : SocketException(java.net.SocketException) InetAddress(java.net.InetAddress) PlainSocketImpl(java.net.PlainSocketImpl)

Aggregations

InetAddress (java.net.InetAddress)2214 UnknownHostException (java.net.UnknownHostException)422 IOException (java.io.IOException)294 Test (org.junit.Test)289 InetSocketAddress (java.net.InetSocketAddress)250 NetworkInterface (java.net.NetworkInterface)191 ArrayList (java.util.ArrayList)173 SocketException (java.net.SocketException)153 HashMap (java.util.HashMap)104 Inet6Address (java.net.Inet6Address)103 Inet4Address (java.net.Inet4Address)96 Socket (java.net.Socket)78 DatagramPacket (java.net.DatagramPacket)73 LinkAddress (android.net.LinkAddress)70 Token (org.apache.cassandra.dht.Token)67 DatagramSocket (java.net.DatagramSocket)65 Map (java.util.Map)59 RouteInfo (android.net.RouteInfo)56 LinkProperties (android.net.LinkProperties)52 ServerSocket (java.net.ServerSocket)52