Example usage for java.net InetAddress getByName

List of usage examples for java.net InetAddress getByName

Introduction

In this page you can find the example usage for java.net InetAddress getByName.

Prototype

public static InetAddress getByName(String host) throws UnknownHostException 

Source Link

Document

Determines the IP address of a host, given the host's name.

Usage

From source file:be.fedict.trust.service.util.ClockDriftUtil.java

public static TimeInfo executeNTP(ClockDriftConfigEntity clockDriftConfig, final NetworkConfig networkConfig)
        throws IOException {

    LOG.debug("clock drift detection: " + clockDriftConfig.toString());

    final InetAddress ntpServerAddress = InetAddress.getByName(clockDriftConfig.getServer());

    TimeInfo timeInfo;// w  ww. j a  v  a2 s .  c  o m
    NTPUDPClient client = new NTPUDPClient();
    client.setDefaultTimeout(clockDriftConfig.getTimeout());
    client.open();
    LOG.debug("NTP server: " + ntpServerAddress);
    timeInfo = client.getTime(ntpServerAddress);
    client.close();
    timeInfo.computeDetails();
    return timeInfo;
}

From source file:com.mirth.connect.connectors.tcp.SocketUtil.java

/**
 * Creates a socket and connects it to the specified remote host on the specified remote port.
 * //  w w  w  .ja v  a2s  .c  o  m
 * @param host
 *            - The remote host to connect on.
 * @param port
 *            - The remote port to connect on.
 * @param localAddr
 *            - The local address to bind the socket to.
 * @param localPort
 *            - The local port to bind the socket to.
 * @param timeout
 *            - The socket timeout to use when connecting.
 * @return The bound and connected Socket.
 * @throws UnknownHostException
 *             if the IP address of the host could not be determined
 * @throws IOException
 *             if an I/O error occurs when creating the socket
 */
public static Socket createSocket(TcpConfiguration configuration, String localAddr, int localPort)
        throws UnknownHostException, IOException {
    Socket socket = configuration.createSocket();

    if (StringUtils.isNotEmpty(localAddr)) {
        InetAddress localAddress = InetAddress.getByName(TcpUtil.getFixedHost(localAddr));
        socket.bind(new InetSocketAddress(localAddress, localPort));
    }

    return socket;
}

From source file:com.madgag.agit.GitTestUtils.java

public static String gitServerHostAddress() throws IOException, FileNotFoundException, UnknownHostException {
    File bang = new File(Environment.getExternalStorageDirectory(), "agit-integration-test.properties");
    Properties properties = new Properties();
    if (bang.exists()) {
        properties.load(new FileReader(bang));
    }/*www.j  a  v a 2 s .  c  o m*/
    String hostAddress = properties.getProperty("gitserver.host.address", "10.0.2.2");
    InetAddress address = InetAddress.getByName(hostAddress);
    assertThat("Test gitserver host " + hostAddress + " is reachable", address.isReachable(1000), is(true));
    return hostAddress;
}

From source file:org.springframework.cloud.sleuth.stream.Host.java

public int getIpv4() {
    InetAddress inetAddress = null;
    try {//from   ww w .j av a2  s .  com
        inetAddress = InetAddress.getByName(this.address);
    } catch (final UnknownHostException e) {
        throw new IllegalArgumentException(e);
    }
    return ByteBuffer.wrap(inetAddress.getAddress()).getInt();
}

From source file:beans.TimeClient.java

public static final void timeUDP(String host) throws IOException {
    TimeUDPClient client = new TimeUDPClient();

    // We want to timeout if a response takes longer than 60 seconds
    client.setDefaultTimeout(60000);//from ww w.j  av  a2  s.  c o m
    client.open();
    System.out.println(client.getDate(InetAddress.getByName(host)));
    client.close();
}

From source file:com.adaptris.core.stubs.ExternalResourcesHelper.java

public static boolean isExternalServerAvailable(String server, int port) {
    boolean result = false;
    try (Socket s = new Socket()) {
        // Try and get a socket to dev on port 80
        // don't give it more than a second though...
        InetSocketAddress addr = new InetSocketAddress(InetAddress.getByName(server), port);
        s.connect(addr, 1000);//w w  w  .  j  a va  2s. com
        result = true;
    } catch (Exception e) {
    }
    return result;
}

From source file:me.xingrz.prox.ProxVpnService.java

private static InetAddress getAddressQuietly(String address) {
    try {//w ww.  ja  va  2 s .c  om
        return InetAddress.getByName(address);
    } catch (UnknownHostException e) {
        return null;
    }
}

From source file:net.sf.antcontrib.net.httpclient.HostConfig.java

public void setAddress(String address) {
    try {//from w w w  . j a  v  a2s. com
        setLocalAddress(InetAddress.getByName(address));
    } catch (UnknownHostException e) {
        throw new BuildException(e);
    }
}

From source file:biz.karms.sinkit.ejb.util.CIDRUtils.java

public static ImmutablePair<String, String> getStartEndAddresses(final String cidr)
        throws UnknownHostException {
    //TODO: This is silly. Refactor CIDRUtils so as to accept actual IPs as well as subnets.
    //TODO: Validate the thing before processing. Guava?
    final String fixedCIDR;
    if (!cidr.contains("/")) {
        //IPv6? Hmmm...
        if (cidr.contains(":")) {
            fixedCIDR = cidr + "/128";
        } else {/*  w ww . j  a v a2s. c o m*/
            fixedCIDR = cidr + "/32";
        }
    } else {
        fixedCIDR = cidr;
    }
    final int index = fixedCIDR.indexOf("/");
    final InetAddress inetAddress = InetAddress.getByName(fixedCIDR.substring(0, index));
    final int prefixLength = Integer.parseInt(fixedCIDR.substring(index + 1));

    final ByteBuffer maskBuffer;
    if (inetAddress.getAddress().length == 4) {
        maskBuffer = ByteBuffer.allocate(4).putInt(-1);
    } else {
        maskBuffer = ByteBuffer.allocate(16).putLong(-1L).putLong(-1L);
    }

    final BigInteger mask = (new BigInteger(1, maskBuffer.array())).not().shiftRight(prefixLength);
    final ByteBuffer buffer = ByteBuffer.wrap(inetAddress.getAddress());
    final BigInteger ipVal = new BigInteger(1, buffer.array());
    final BigInteger startIp = ipVal.and(mask);
    final BigInteger endIp = startIp.add(mask.not());

    return new ImmutablePair<>(String.format("%040d", startIp), String.format("%040d", endIp));
}

From source file:org.elasticsoftware.elasticactors.configuration.ClusteringConfiguration.java

@Bean(name = "clusterService")
public ClusterService createClusterService() throws UnknownHostException {
    String nodeId = env.getRequiredProperty("ea.node.id");
    InetAddress nodeAddress = InetAddress.getByName(env.getRequiredProperty("ea.node.address"));
    int nodePort = env.getProperty("ea.node.port", Integer.class, 9090);
    String clusterName = env.getRequiredProperty("ea.cluster");
    String discoveryNodes = env.getProperty("ea.cluster.discovery.nodes", String.class, null);
    return new ShoalClusterService(clusterName, nodeId, nodeAddress, nodePort, discoveryNodes);
}