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.netvirt.dhcpservice.api.DHCPOptions.java

public InetAddress getOptionInetAddr(byte code) {
    byte[] opt = this.getOptionBytes(code);
    try {/*from   w ww  . j  a  v a2s . c om*/
        return InetAddress.getByAddress(opt);
    } catch (UnknownHostException | NullPointerException e) {
        return null;
    }
}

From source file:com.offbynull.portmapper.pcp.ThirdPartyPcpOption.java

/**
 * Constructs a {@link ThirdPartyPcpOption} by parsing a buffer.
 * @param buffer buffer containing PCP option data
 * @throws NullPointerException if any argument is {@code null}
 * @throws BufferUnderflowException if not enough data is available in {@code buffer}
 * @throws IllegalArgumentException if option code is not {@code 1}
 *///from w  w w .j a va  2  s  .c  o  m
public ThirdPartyPcpOption(ByteBuffer buffer) {
    super(buffer);
    Validate.isTrue(super.getCode() == 1);
    byte[] addrArr = new byte[16];
    buffer.get(addrArr);
    try {
        internalIpAddress = InetAddress.getByAddress(addrArr);
    } catch (UnknownHostException uhe) {
        throw new IllegalStateException(uhe); // should never happen
    }
}

From source file:org.apache.hadoop.mapred.TrackerClientCache.java

/**
 * Connect to the task tracker and get the RPC client.
 * @param host The host./*from  w  w w  . j  a v  a2s  .  co m*/
 * @param port the port.
 * @return The RPC client.
 * @throws IOException
 */
private CoronaTaskTrackerProtocol createClient(String host, int port) throws IOException {
    String staticHost = NetUtils.getStaticResolution(host);
    InetSocketAddress s = null;
    InetAddress inetAddress = null;
    byte[] byteArr = null;
    if (staticHost != null) {
        inetAddress = InetAddress.getByName(staticHost);
    } else {
        byteArr = Utilities.asBytes(host);
        if (byteArr == null) {
            inetAddress = InetAddress.getByName(host);
        } else {
            inetAddress = InetAddress.getByAddress(byteArr);
        }
    }
    s = new InetSocketAddress(inetAddress, port);
    LOG.info("Creating client to " + (staticHost != null ? staticHost : host) + ":" + s.getPort());
    long connectTimeout = conf.getLong(CoronaJobTracker.TT_CONNECT_TIMEOUT_MSEC_KEY, 10000L);
    int rpcTimeout = conf.getInt(CoronaJobTracker.TT_RPC_TIMEOUT_MSEC_KEY, 60000);
    return RPC.waitForProxy(CoronaTaskTrackerProtocol.class, CoronaTaskTrackerProtocol.versionID, s, conf,
            connectTimeout, rpcTimeout);
}

From source file:com.irccloud.android.GingerbreadImageProxy.java

public void init() {
    try {/*from  w  w w.j  a  v a  2s.c o m*/
        socket = new ServerSocket(port, 0, InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 }));
        socket.setSoTimeout(5000);
        port = socket.getLocalPort();
    } catch (UnknownHostException e) {
        Log.e(LOG_TAG, "Error initializing server", e);
    } catch (IOException e) {
        Log.e(LOG_TAG, "Error initializing server", e);
    }
}

From source file:com.cafbit.multicasttest.NetThread.java

/**
 * The main network loop.  Multicast DNS packets are received,
 * processed, and sent to the UI./*  ww w.  j  av  a 2 s.  co  m*/
 * 
 * This loop may be interrupted by closing the multicastSocket,
 * at which time any commands in the commandQueue will be
 * processed.
 */
@Override
public void run() {
    Log.v(TAG, "starting network thread");

    localAddresses = NetUtil.getLocalAddresses();
    multicastLock = null;

    // initialize the network
    try {
        networkInterface = netUtil.getFirstWifiOrEthernetInterface();
        if (networkInterface == null) {
            throw new IOException("Your WiFi is not enabled.");
        }
        groupAddress = InetAddress.getByAddress(MDNS_ADDR);

        WifiManager wm = (WifiManager) MulticastTestActivity.mThis
                .getSystemService(MulticastTestActivity.mThis.getApplicationContext().WIFI_SERVICE);
        WifiManager.MulticastLock multicastLock = wm.createMulticastLock("mydebuginfo");
        multicastLock.acquire();

        openSocket();
    } catch (IOException e1) {
        Log.i(TAG, "IOException : " + e1.getMessage());
        return;
    }

    mReceivedTask = new ReceivedTask();
    mReceivedTask.execute("");
    try {
        mReceivedTask.get();
    } catch (InterruptedException e1) {

    } catch (ExecutionException e1) {
    }

}

From source file:org.lwes.EventTest.java

@Test
public void testGetInetAddressAsBytes() throws EventSystemException, UnknownHostException {
    Event evt = createEvent();//from   w  w w . j  a  v a  2 s  .  co  m
    evt.setEventName("Test");
    final InetAddress localhost = InetAddress.getLocalHost();
    evt.setByteArray("ip", localhost.getAddress());
    byte[] iparr = evt.getByteArray("ip");
    InetAddress a = InetAddress.getByAddress(iparr);
    assertNotNull(a);
    assertEquals(localhost, a);
}

From source file:at.tugraz.ist.akm.networkInterface.WifiIpAddress.java

@SuppressWarnings("deprecation")
private String readWifiIP4Address() {
    String ip4Address = "0.0.0.0";
    byte[] ipAddress = BigInteger.valueOf(mWifiManager.getConnectionInfo().getIpAddress()).toByteArray();
    try {//from   w w w.j a  v  a 2s .c om
        InetAddress address = InetAddress.getByAddress(ipAddress);
        String concreteAddressString = address.getHostAddress().toUpperCase(Locale.getDefault());
        if (InetAddressUtils.isIPv4Address(concreteAddressString)) {
            ip4Address = Formatter.formatIpAddress(mWifiManager.getConnectionInfo().getIpAddress());
        }
    } catch (UnknownHostException e) {
        return ip4Address;
    }
    return ip4Address;
}

From source file:com.vuze.plugin.azVPN_Helper.CheckerCommon.java

public CheckerCommon(PluginInterface pi) {
    this.pi = pi;
    this.config = pi.getPluginconfig();
    this.texts = pi.getUtilities().getLocaleUtilities();

    try {// w  w w .j a  v a  2 s  .co m
        testSocketAddress = InetAddress.getByAddress(new byte[] { 8, 8, 8, 8 });
    } catch (UnknownHostException e) {
    }

}

From source file:com.neophob.sematrix.core.output.E1_31Device.java

@Override
protected void sendBufferToReceiver(int universeId, byte[] buffer) {
    if (this.initialized) {
        byte[] data = dataPacket.assembleNewE131Packet(this.sequenceID++, universeId, buffer);
        packet.setData(data);//from w ww . j  av a  2s .  c o  m
        packet.setLength(data.length);

        if (this.sendMulticast) {
            // multicast - universe number must be in lower 2 bytes
            byte[] addr = new byte[4];
            addr[0] = (byte) 239;
            addr[1] = (byte) 255;
            addr[2] = (byte) (universeId >> 8);
            addr[3] = (byte) (universeId & 255);
            InetAddress iaddr;
            try {
                iaddr = InetAddress.getByAddress(addr);
                packet.setAddress(iaddr);
            } catch (UnknownHostException e) {
                LOG.log(Level.WARNING, "Failed to set target address!", e);
            }
        }
        try {
            dsocket.send(packet);
        } catch (IOException e) {
            errorCounter++;
            LOG.log(Level.WARNING, "failed to send E1.31 data.", e);
        }
    }
}

From source file:net.straylightlabs.archivo.model.Tivo.java

/**
 * Create a new Tivo object from a JSON String.
 *
 * @param json String containing the Tivo object in JSON
 * @param mak  Media access key to use for the resulting Tivo
 * @return A new Tivo object/*from   w w  w.j a va 2  s  .c  om*/
 * @throws IllegalArgumentException
 */
public static Tivo fromJSON(final String json, final String mak) throws IllegalArgumentException {
    JSONObject jo = new JSONObject(json);
    String name = jo.getString(JSON_NAME);
    String tsn = jo.getString(JSON_TSN);
    int port = jo.getInt(JSON_PORT);
    JSONArray jsonAddresses = jo.getJSONArray(JSON_ADDRESSES);
    Set<InetAddress> addresses = new HashSet<>();
    Base64.Decoder decoder = Base64.getDecoder();
    for (int i = 0; i < jsonAddresses.length(); i++) {
        try {
            addresses.add(InetAddress.getByAddress(decoder.decode(jsonAddresses.getString(i))));
        } catch (UnknownHostException e) {
            throw new IllegalArgumentException("TiVo address in invalid: " + e.getLocalizedMessage());
        }
    }

    return new Builder().name(name).tsn(tsn).port(port).addresses(addresses).mak(mak).build();
}