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:Main.java

public static String getServiceIp(String host) {
    InetAddress myServer = null;/*  ww  w.ja v  a 2  s . c o  m*/
    try {
        myServer = InetAddress.getByName(host);
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }
    if (myServer == null) {
        return "0";
    }
    return myServer.getHostAddress();
}

From source file:Main.java

/**
 * Method to check if internet is available.
 *
 * @return Returns true if the internet is available
 *///from   w w  w.j av a  2s  .  c o  m
public static boolean isInternetAvailable() {
    try {
        InetAddress address = InetAddress.getByName("google.com");
        return !address.equals("");
    } catch (Exception e) {
        return false;
    }
}

From source file:Main.java

public static String getHostName(String ip) {
    String hostName = "Unknown";

    try {//from www  . j  a va  2  s.  c  o  m
        InetAddress add = InetAddress.getByName(ip);
        hostName = add.getHostName();

        Log.d(Tag, "host name is " + hostName);
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return hostName;
}

From source file:Main.java

/**
 * Checks validity of a dotted IP address string.
 * Uses {@link java.net.InetAddress}.getByName() method.
 *
 * @param ipStr/*from   w  w w  . j  av a2  s.  co m*/
 * @return
 */
public static boolean isValidIp(String ipStr) {
    try {
        InetAddress ip = InetAddress.getByName(ipStr);
        return ip2Long(ipStr) > 0;
    } catch (Exception e) {
        return false;
    }
}

From source file:Main.java

/**
 * hostname from string to int/* www . j av a  2 s .  com*/
 * @param hostname
 * @return
 */
public static int lookupHost(String hostname) {
    InetAddress inetAddress;
    try {
        inetAddress = InetAddress.getByName(hostname);
    } catch (UnknownHostException e) {
        return -1;
    }
    byte[] addrBytes;
    int addr;
    addrBytes = inetAddress.getAddress();
    addr = ((addrBytes[3] & 0xff) << 24) | ((addrBytes[2] & 0xff) << 16) | ((addrBytes[1] & 0xff) << 8)
            | (addrBytes[0] & 0xff);
    return addr;
}

From source file:Applicant.java

public static void main(String[] args) throws ClassNotFoundException, SQLException, UnknownHostException,
        ExecutionException, InterruptedException {
    //        Class.forName("com.mysql.jdbc.Driver");
    //        Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306/customer","root","sachin123@");
    //        Statement stmt=con.createStatement();
    //        int Size=0,i=1;
    //        ResultSet rs=stmt.executeQuery("select count(*) from personal limit 100");
    //        while(rs.next())
    //        {//  w ww .  j  a v a  2 s. c o m
    //            Size=rs.getInt(1);
    //            System.out.println(Size);
    //        }
    //
    //
    //        con.close();

    Applicant input = new Applicant();
    input.setCust_No("CU00008333343056");
    input.setFirst_Name("mer");
    input.setLast_Name("Braun");
    input.setStreet("Fangdieckstrae 14");
    input.setTown("Koblenz");
    input.setZipcode("57474  ");
    input.setId("AVtcqDtr-hedAcx8Wh2Y");

    Settings settings = Settings.builder().put("cluster.name", "jvmti-cluster")
            .put("client.transport.sniff", true).build();
    PreBuiltTransportClient client = new PreBuiltTransportClient(settings);

    client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));

    Map<String, Object> f = new HashMap<String, Object>();
    f.put("data", "hello");
    UpdateRequest updateRequest = new UpdateRequest("a", "b", "1")
            .script(new Script(ScriptType.INLINE, "painless", "ctx._source.doc=params.data", f));
    client.update(updateRequest).get();

    SearchResponse sr = client.prepareSearch("customer").setTypes("personal_info")
            .setQuery(QueryBuilders.matchQuery("FirstName", input.getFirst_Name())).execute().get();
    Match_Record.updataInput(sr, input, client, "first_name_rel");
    sr = client.prepareSearch("customer").setTypes("personal_info")
            .setQuery(QueryBuilders.matchQuery("LastName", input.getLast_Name())).execute().get();
    Match_Record.updataInput(sr, input, client, "last_name_rel");
    sr = client.prepareSearch("customer").setTypes("personal_info")
            .setQuery(QueryBuilders.matchQuery("Street", input.getStreet())).execute().get();
    Match_Record.updataInput(sr, input, client, "street_rel");
    sr = client.prepareSearch("customer").setTypes("personal_info")
            .setQuery(QueryBuilders.matchQuery("Zip", input.getZipcode())).execute().get();
    Match_Record.updataInput(sr, input, client, "zip_rel");
    sr = client.prepareSearch("customer").setTypes("personal_info")
            .setQuery(QueryBuilders.matchQuery("Place_of_Residence", input.getTown())).execute().get();
    Match_Record.updataInput(sr, input, client, "place_of_residence_rel");

    UpdateByQueryRequestBuilder updateByQuery = UpdateByQueryAction.INSTANCE.newRequestBuilder(client);

    f.put("data", input.getCust_No());

    BulkIndexByScrollResponse response = updateByQuery.source("customer")
            .filter(termQuery("FirstName", input.getFirst_Name()))
            .script(new Script(ScriptType.INLINE, "painless", "ctx._source.FirstName_Rel.add(params.data)", f))
            .get();
    response = updateByQuery.source("customer").filter(termQuery("LastName", input.getLast_Name()))
            .script(new Script(ScriptType.INLINE, "painless", "ctx._source.LastName_Rel.add(params.data)", f))
            .get();
    response = updateByQuery.source("customer").filter(termQuery("Zip", input.getZipcode()))
            .script(new Script(ScriptType.INLINE, "painless", "ctx._source.Zipcode_Rel.add(params.data)", f))
            .get();
    response = updateByQuery.source("customer").filter(termQuery("Place_of_Residence", input.getTown()))
            .script(new Script(ScriptType.INLINE, "painless", "ctx._source.Town_Rel.add(params.data)", f))
            .get();
    response = updateByQuery.source("customer").filter(termQuery("Street", input.getStreet()))
            .script(new Script(ScriptType.INLINE, "painless", "ctx._source.Street_Rel.add(params.data)", f))
            .get();

    //        UpdateRequest updateRequest = new UpdateRequest("a", "b", "1")
    //                .script(new Script(ScriptType.INLINE,"painless","ctx._source.check.add(params.data)",f));
    //        client.update(updateRequest).get();

    client.close();
}

From source file:Main.java

static public boolean isValidIpAddress(String ip) {
    boolean v4 = InetAddressUtils.isIPv4Address(ip);
    boolean v6 = InetAddressUtils.isIPv6Address(ip);
    if (!v4 && !v6)
        return false;
    try {/*  www.  ja  v a 2s  . co m*/
        InetAddress inet = InetAddress.getByName(ip);
        return inet.isLinkLocalAddress() || inet.isSiteLocalAddress();
    } catch (UnknownHostException e) {
        //Log.e(TAG, e.toString());
        return false;
    }
}

From source file:edu.umass.cs.reconfiguration.reconfigurationpackets.CreateServiceName.java

public static void main(String[] args) {
    try {/*from   ww w.  j  ava 2s .  c om*/
        Util.assertAssertionsEnabled();
        InetSocketAddress isa = new InetSocketAddress(InetAddress.getByName("localhost"), 2345);
        int numNames = 1000;
        String[] reconfigurators = { "RC43", "RC22", "RC78", "RC21", "RC143" };
        String namePrefix = "someName";
        String defaultState = "default_initial_state";
        String[] names = new String[numNames];
        String[] states = new String[numNames];
        for (int i = 0; i < numNames; i++) {
            names[i] = namePrefix + i;
            states[i] = defaultState + i;
        }
        CreateServiceName bcreate1 = new CreateServiceName(isa, "random0", 0, "hello");
        HashMap<String, String> nameStates = new HashMap<String, String>();
        for (int i = 0; i < names.length; i++)
            nameStates.put(names[i], states[i]);
        CreateServiceName bcreate2 = new CreateServiceName(isa, names[0], 0, states[0], nameStates);
        System.out.println(bcreate1.toString());
        System.out.println(bcreate2.toString());

        // translate a batch into consistent constituent batches
        Collection<Set<String>> batches = ConsistentReconfigurableNodeConfig.splitIntoRCGroups(
                new HashSet<String>(Arrays.asList(names)), new HashSet<String>(Arrays.asList(reconfigurators)));
        int totalSize = 0;
        int numBatches = 0;
        for (Set<String> batch : batches)
            System.out.println("batch#" + numBatches++ + " of size " + batch.size() + " (totalSize = "
                    + (totalSize += batch.size()) + ")" + " = " + batch);
        assert (totalSize == numNames);
        System.out.println(bcreate2.getSummary());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static InetAddress getBroadcastAddress(Context context) throws UnknownHostException {
    WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    DhcpInfo dhcp = wifi.getDhcpInfo();//w w  w. j  a  va 2s  .c om
    if (dhcp == null) {
        return InetAddress.getByName("255.255.255.255");
    }
    int broadcast = (dhcp.ipAddress & dhcp.netmask) | ~dhcp.netmask;
    byte[] quads = new byte[4];
    for (int k = 0; k < 4; k++)
        quads[k] = (byte) ((broadcast >> k * 8) & 0xFF);
    return InetAddress.getByAddress(quads);
}

From source file:Main.java

public static void printAddressDetails(String host) throws Exception {
    System.out.println("Host '" + host + "' details starts...");
    InetAddress addr = InetAddress.getByName(host);
    System.out.println("Host  IP  Address: " + addr.getHostAddress());
    System.out.println("Canonical  Host  Name: " + addr.getCanonicalHostName());

    int timeOutinMillis = 10000;
    System.out.println("isReachable(): " + addr.isReachable(timeOutinMillis));
    System.out.println("isLoopbackAddress(): " + addr.isLoopbackAddress());

}