Example usage for java.net InetAddress getByAddress

List of usage examples for java.net InetAddress getByAddress

Introduction

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

Prototype

public static InetAddress getByAddress(byte[] addr) throws UnknownHostException 

Source Link

Document

Returns an InetAddress object given the raw IP address .

Usage

From source file:org.opendaylight.controller.protocol_plugin.openflow.vendorextension.v6extension.V6Match.java

@Override
public V6Match clone() {

    V6Match ret = (V6Match) super.clone();
    try {//ww  w.  j a  v a2  s . c  o m
        if (this.nwSrc != null) {
            ret.nwSrc = InetAddress.getByAddress(this.nwSrc.getAddress());
        }
        if (this.nwDst != null) {
            ret.nwDst = InetAddress.getByAddress(this.nwDst.getAddress());
        }
        return ret;
    } catch (UnknownHostException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.cloud.utils.net.NetUtils.java

public static String getIp6FromRange(final String ip6Range) {
    final String[] ips = ip6Range.split("-");
    final String startIp = ips[0];
    final IPv6Address start = IPv6Address.fromString(startIp);
    final BigInteger gap = countIp6InRange(ip6Range);
    BigInteger next = new BigInteger(gap.bitLength(), s_rand);
    while (next.compareTo(gap) >= 0) {
        next = new BigInteger(gap.bitLength(), s_rand);
    }/*from w ww .  j a va 2  s  . c  o  m*/
    InetAddress resultAddr = null;
    final BigInteger startInt = convertIPv6AddressToBigInteger(start);
    if (startInt != null) {
        final BigInteger resultInt = startInt.add(next);
        try {
            resultAddr = InetAddress.getByAddress(resultInt.toByteArray());
        } catch (final UnknownHostException e) {
            return null;
        }
    }
    if (resultAddr != null) {
        final IPv6Address ip = IPv6Address.fromInetAddress(resultAddr);
        return ip.toString();
    }
    return null;
}

From source file:net.networksaremadeofstring.rhybudd.ZenossAPI.java

public static String ntoa(long raw) {
    byte[] b = new byte[] { (byte) (raw >> 24), (byte) (raw >> 16), (byte) (raw >> 8), (byte) raw };

    try {//from  w ww .j av  a 2 s.  com
        return InetAddress.getByAddress(b).getHostAddress();
    } catch (UnknownHostException e) {
        //No way here
        return null;
    }
}

From source file:org.midonet.brain.services.vxgw.VxLanFloodingProxyTest.java

public static InetAddress getHostAddress(int value) throws UnknownHostException {
    return InetAddress.getByAddress(
            new byte[] { HOST_IP[0], HOST_IP[1], (byte) ((value >> 8) & 0xFF), (byte) (value & 0xFF) });
}

From source file:org.smilec.smile.student.CourseList.java

private void logError(String url, String err) {
    boolean reachable = false;
    String internetTest = "";
    String str = "";
    Date now = new Date();

    internetTest = now.toString() + ", loading page " + url + " error: " + err + "\n";
    ;//from w  ww .  j  av a 2 s  .  c o m
    System.out.println(internetTest);

    try {
        //test router
        //InetAddress address = InetAddress.getByName("192.168.2.1");
        byte[] b = new byte[] { (byte) 192, (byte) 168, (byte) 2, (byte) 1 };
        InetAddress address = InetAddress.getByAddress(b);

        // Try to reach the specified address within the timeout
        // periode. If during this periode the address cannot be
        // reach then the method returns false.
        reachable = address.isReachable(5000); // 5 seconds            
    } catch (Exception e) {
        reachable = false;
        e.printStackTrace();
    }

    str = "Is router (192.168.2.1) reachable? " + reachable + "\n";
    System.out.println(str);
    internetTest += str;

    try {
        //test server
        InetAddress address = InetAddress.getByName(cururi);
        reachable = address.isReachable(5000);

    } catch (Exception e) {
        reachable = false;
        e.printStackTrace();
    }

    str = "Is host reachable? " + reachable + "\n";
    System.out.println(str);
    internetTest += str;
    String received = "";
    int statusCode = 0;

    try {
        HttpParams params = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(params, 5000); // until first connection
        HttpConnectionParams.setSoTimeout(params, 5000); // 10000 ms socket timeout --? no time out for socket
        HttpClient httpclient = new DefaultHttpClient(params);
        HttpGet httpget;
        httpget = new HttpGet("http://" + cururi);
        HttpResponse response = httpclient.execute(httpget);
        statusCode = response.getStatusLine().getStatusCode();

        if (statusCode == 404) {
            // server not ready
            // do nothing                           
            throw new Exception("" + response.getStatusLine());
        } else if ((statusCode / 100) != 2) {
            throw new Exception("" + response.getStatusLine());
        } else {
            BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            String line;
            StringBuffer sb = new StringBuffer("");
            while ((line = in.readLine()) != null) {
                sb.append(line);
            }

            received = sb.toString();

            if (received != null && received.length() > 0)
                reachable = true;

            received = "http status code: " + statusCode;
        }

    } catch (Exception e) {
        //e.printStackTrace();
        received = "loading error, http status code: " + statusCode;
        reachable = false;
    }

    str = "Is Apache reachable? " + reachable + ", " + received + "\n";
    System.out.println(str);
    internetTest += str;

    try {
        // open myfilename.txt for writing
        Log.d("Write log Err", "Start writing error log");
        OutputStreamWriter out = new OutputStreamWriter(openFileOutput("smileErrorLog.txt", 0));
        out.write(internetTest);
        out.close();
        Log.d("Write log Err", "Writing error log finished.");
    } catch (java.io.IOException e) {
        //do something if an IOException occurs.
        Log.d("Write log Error", "ERROR");
        e.printStackTrace();
    }

}

From source file:es.caib.seycon.ng.servei.XarxaServiceImpl.java

private XarxaEntity guessNetwork(byte[] b) {
    XarxaEntityDao dao = getXarxaEntityDao();
    XarxaEntity xarxa = null;//from   ww w . j ava  2  s  . c o  m
    for (int bc = b.length - 1; xarxa == null && bc >= 0; bc--) {
        byte mascara = (byte) 255;
        for (int bits = 0; xarxa == null && bits < 8; bits++) {
            mascara = (byte) (mascara << 1);
            b[bc] = (byte) (b[bc] & mascara);
            InetAddress addr2;
            try {
                addr2 = InetAddress.getByAddress(b);
                String addrText = addr2.getHostAddress();
                xarxa = dao.findByAdreca(addrText);
            } catch (java.net.UnknownHostException e) {
                e.printStackTrace();
            }
        }
    }
    if (xarxa == null) {
        String defaultNetwork = System.getProperty("soffid.network.internet"); //$NON-NLS-1$
        if (defaultNetwork != null) {
            xarxa = dao.findByCodi(defaultNetwork);
            if (xarxa == null) {
                xarxa = dao.newXarxaEntity();
                xarxa.setCodi(defaultNetwork);
                xarxa.setAdreca("0.0.0.0"); //$NON-NLS-1$
                xarxa.setMascara("255.255.255.255"); //$NON-NLS-1$
                xarxa.setDchpSupport(true);
                xarxa.setDescripcio("Autocreated network for unknown IP adresses"); //$NON-NLS-1$
                xarxa.setNormalitzada("N"); //$NON-NLS-1$
                dao.create(xarxa);
            }
        }
    }
    return xarxa;
}