Example usage for java.net InetAddress getAddress

List of usage examples for java.net InetAddress getAddress

Introduction

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

Prototype

public byte[] getAddress() 

Source Link

Document

Returns the raw IP address of this InetAddress object.

Usage

From source file:com.material.katha.wifidirectmp3.DeviceDetailFragment.java

public byte[] getLocalIPAddress() {
    try {//from w w w  .ja v  a2s.co m
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    if (inetAddress instanceof Inet4Address) {
                        return inetAddress.getAddress();
                    }
                }
            }
        }
    } catch (SocketException ex) {
        // Log.e("AndroidNetworkAddressFactory", "getLocalIPAddress()", ex);
    } catch (NullPointerException ex) {
        // Log.e("AndroidNetworkAddressFactory", "getLocalIPAddress()", ex);
    }
    return null;
}

From source file:org.rifidi.edge.adapter.csl.util.CslRfidTagServer.java

private boolean setMode(ReaderMode mode) {
    try {// w w w  . j av  a 2 s .c o  m

        InetAddress IPInet = InetAddress.getByName(this.IPAddress);

        // Set low-level API mode
        String cmd = "80" + byteArrayToHexString(IPInet.getAddress(), IPInet.getAddress().length)
                + (mode == ReaderMode.lowLevel ? "000C" : "000D");
        logger.info("Set low-level API with command 0x" + cmd.toString());

        DatagramPacket sendPacket = new DatagramPacket(hexStringToByteArray(cmd),
                hexStringToByteArray(cmd).length, IPInet, 3041);
        byte[] receiveData = new byte[4];
        DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
        clientSocket.send(sendPacket);
        clientSocket.setSoTimeout(2000);
        clientSocket.receive(receivePacket);
        logger.info("Return: " + byteArrayToHexString(receivePacket.getData(), receivePacket.getData().length));

        if (byteArrayToHexString(receivePacket.getData(), receivePacket.getData().length).startsWith("810100"))
            return true;
        else
            return false;
    } catch (UnknownHostException e) {
        return false;
    } catch (IOException e) {
        return false;
    }
}

From source file:org.apache.htrace.impl.ZipkinSpanReceiver.java

/**
 * Set up the HTrace to Zipkin converter.
 *///from   www.  j a va  2 s. c o  m
private void initConverter() {
    InetAddress tracedServiceHostname = null;
    // Try and get the hostname.  If it's not configured try and get the local hostname.
    try {
        //TODO (clehene) extract conf to constant
        //TODO (clehene) has this been deprecated?
        String host = conf.get("zipkin.traced-service-hostname", InetAddress.getLocalHost().getHostAddress());
        tracedServiceHostname = InetAddress.getByName(host);
    } catch (UnknownHostException e) {
        LOG.error("Couldn't get the localHost address", e);
    }
    short tracedServicePort = (short) conf.getInt("zipkin.traced-service-port", -1);
    byte[] address = tracedServiceHostname != null ? tracedServiceHostname.getAddress()
            : InetAddress.getLoopbackAddress().getAddress();
    int ipv4 = ByteBuffer.wrap(address).getInt();
    this.converter = new HTraceToZipkinConverter(ipv4, tracedServicePort);
}

From source file:org.rifidi.edge.adapter.csl.util.CslRfidTagServer.java

private ReaderMode checkMode() {
    try {//  ww w  .jav  a 2 s.  c  om

        InetAddress IPInet = InetAddress.getByName(this.IPAddress);

        // Set low-level API mode
        String cmd = "80" + byteArrayToHexString(IPInet.getAddress(), IPInet.getAddress().length) + "000E";
        logger.info("Check mode with command 0x" + cmd.toString());

        DatagramPacket sendPacket = new DatagramPacket(hexStringToByteArray(cmd),
                hexStringToByteArray(cmd).length, IPInet, 3041);
        byte[] receiveData = new byte[5];
        DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
        clientSocket.send(sendPacket);
        clientSocket.setSoTimeout(2000);
        clientSocket.receive(receivePacket);
        logger.info(String.format(
                "Return: " + byteArrayToHexString(receivePacket.getData(), receivePacket.getData().length)));

        if (byteArrayToHexString(receivePacket.getData(), receivePacket.getData().length)
                .startsWith("8101010E00"))
            return ReaderMode.highLevel;
        else if (byteArrayToHexString(receivePacket.getData(), receivePacket.getData().length)
                .startsWith("8101010E01"))
            return ReaderMode.lowLevel;
        else
            return null;
    } catch (UnknownHostException e) {
        return null;
    } catch (IOException e) {
        return null;
    }
}

From source file:com.jagornet.dhcp.db.JdbcIaPrefixDAO.java

public List<IaPrefix> findUnusedByRange(final InetAddress startAddr, final InetAddress endAddr) {
    final long offerExpiration = new Date().getTime() - 12000; // 2 min = 120 sec = 12000 ms
    return getJdbcTemplate().query("select * from iaprefix" + " where ((state=" + IaPrefix.ADVERTISED
            + " and starttime <= ?)" + " or (state=" + IaPrefix.EXPIRED + " or state=" + IaPrefix.RELEASED
            + "))" + " and prefixaddress >= ? and prefixaddress <= ?"
            + " order by state, validendtime, ipaddress", new PreparedStatementSetter() {
                @Override//  w  w w  .j  a  v a  2  s  .  c  o  m
                public void setValues(PreparedStatement ps) throws SQLException {
                    java.sql.Timestamp ts = new java.sql.Timestamp(offerExpiration);
                    ps.setTimestamp(1, ts);
                    ps.setBytes(2, startAddr.getAddress());
                    ps.setBytes(3, endAddr.getAddress());
                }
            }, new IaPrefixRowMapper());
}

From source file:com.alibaba.dragoon.stat.WebURIStatistic.java

public boolean addIp(String ip) {
    if (ip == null || ip.length() == 0) {
        return false;
    }/*ww  w . j  av a 2 s . co m*/
    int commaIndex = ip.indexOf(',');
    if (commaIndex != -1) {
        ip = ip.substring(0, commaIndex);
    }

    InetAddress addr;
    try {
        addr = InetAddress.getByName(ip);
    } catch (UnknownHostException e) {
        return false;
    }

    int ipValue = bytesToInt(addr.getAddress());
    return addIp(ipValue);
}

From source file:com.jagornet.dhcp.db.JdbcIaAddressDAO.java

public List<IaAddress> findUnusedByRange(final InetAddress startAddr, final InetAddress endAddr) {
    final long offerExpiration = new Date().getTime() - 12000; // 2 min = 120 sec = 12000 ms
    return getJdbcTemplate().query(
            "select * from iaaddress" + " where ((state=" + IaAddress.ADVERTISED + " and starttime <= ?)"
                    + " or (state=" + IaAddress.EXPIRED + " or state=" + IaAddress.RELEASED + "))"
                    + " and ipaddress >= ? and ipaddress <= ?" + " order by state, validendtime, ipaddress",
            new PreparedStatementSetter() {
                @Override/*from  w w  w  .  j a  va 2s . c  o m*/
                public void setValues(PreparedStatement ps) throws SQLException {
                    java.sql.Timestamp ts = new java.sql.Timestamp(offerExpiration);
                    ps.setTimestamp(1, ts);
                    ps.setBytes(2, startAddr.getAddress());
                    ps.setBytes(3, endAddr.getAddress());
                }
            }, new IaAddrRowMapper());
}

From source file:eu.stratosphere.nephele.taskmanager.TaskManager.java

/**
 * Find out the TaskManager's own IP address.
 *///from   w  ww.  j a  va2  s. c o  m
private InetAddress getTaskManagerAddress(InetSocketAddress jobManagerAddress) throws IOException {
    AddressDetectionState strategy = AddressDetectionState.ADDRESS;

    while (true) {
        Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
        while (e.hasMoreElements()) {
            NetworkInterface n = e.nextElement();
            Enumeration<InetAddress> ee = n.getInetAddresses();
            while (ee.hasMoreElements()) {
                InetAddress i = ee.nextElement();
                switch (strategy) {
                case ADDRESS:
                    if (hasCommonPrefix(jobManagerAddress.getAddress().getAddress(), i.getAddress())) {
                        if (tryToConnect(i, jobManagerAddress, strategy.getTimeout())) {
                            LOG.info("Determined " + i + " as the TaskTracker's own IP address");
                            return i;
                        }
                    }
                    break;
                case FAST_CONNECT:
                case SLOW_CONNECT:
                    boolean correct = tryToConnect(i, jobManagerAddress, strategy.getTimeout());
                    if (correct) {
                        LOG.info("Determined " + i + " as the TaskTracker's own IP address");
                        return i;
                    }
                    break;
                default:
                    throw new RuntimeException("Unkown address detection strategy: " + strategy);
                }
            }
        }
        // state control
        switch (strategy) {
        case ADDRESS:
            strategy = AddressDetectionState.FAST_CONNECT;
            break;
        case FAST_CONNECT:
            strategy = AddressDetectionState.SLOW_CONNECT;
            break;
        case SLOW_CONNECT:
            throw new RuntimeException("The TaskManager failed to detect its own IP address");
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("Defaulting to detection strategy " + strategy);
        }
    }
}

From source file:com.maxmind.geoip.LookupService.java

public String getOrg(InetAddress addr) {
    return getOrg(bytesToLong(addr.getAddress()));
}

From source file:com.maxmind.geoip.LookupService.java

public int getID(InetAddress ipAddress) {
    return getID(bytesToLong(ipAddress.getAddress()));
}