List of usage examples for java.net InetAddress getByAddress
public static InetAddress getByAddress(byte[] addr) throws UnknownHostException
From source file:ugr.cristian.serverVideoApp.PacketHandler.java
/*********************************/ static private InetAddress intToInetAddress(int i) { byte b[] = new byte[] { (byte) ((i >> 24) & 0xff), (byte) ((i >> 16) & 0xff), (byte) ((i >> 8) & 0xff), (byte) (i & 0xff) }; InetAddress addr;//www . j a v a 2s. co m try { addr = InetAddress.getByAddress(b); } catch (UnknownHostException e) { return null; } return addr; }
From source file:fr.fg.server.action.login.Register.java
private String long2ip(long ip) throws UnknownHostException { byte[] bytes = new byte[4]; bytes[0] = (byte) ((ip & 0xff000000) >> 24); bytes[1] = (byte) ((ip & 0x00ff0000) >> 16); bytes[2] = (byte) ((ip & 0x0000ff00) >> 8); bytes[3] = (byte) (ip & 0x000000ff); return InetAddress.getByAddress(bytes).getHostAddress(); }
From source file:com.drinviewer.droiddrinviewer.DrinViewerBroadcastReceiver.java
private String getWiFiBroadcastAddress(Context context) { String bcastaddr = null;// w w w . ja va 2 s. co m WifiManager mWifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); DhcpInfo dhcp = mWifi.getDhcpInfo(); if (mWifi.isWifiEnabled() && dhcp != null) { 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); try { bcastaddr = InetAddress.getByAddress(quads).getHostAddress(); } catch (UnknownHostException e) { e.printStackTrace(); } } return bcastaddr; }
From source file:org.deviceconnect.android.deviceplugin.irkit.IRKitManager.java
/** * WiFi?IP??.// w w w . j a v a2s.c om * * @param ipValue IP?int * @return InetAddress?????null? */ private InetAddress parseIPAddress(final int ipValue) { byte[] byteaddr = new byte[] { (byte) (ipValue & 0xff), (byte) (ipValue >> 8 & 0xff), (byte) (ipValue >> 16 & 0xff), (byte) (ipValue >> 24 & 0xff) }; try { return InetAddress.getByAddress(byteaddr); } catch (UnknownHostException e) { if (BuildConfig.DEBUG) { e.printStackTrace(); } // IP????????????? return null; } }
From source file:org.basdroid.common.NetworkUtils.java
public static String getMacAddressFromNetworkInterface(final Context context) { WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = wifiManager.getConnectionInfo(); int ipAddress = wifiInfo.getIpAddress(); // Convert little-endian to big-endianif needed if (ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) { ipAddress = Integer.reverseBytes(ipAddress); }//from w w w . j a va 2s . co m byte[] bytes = BigInteger.valueOf(ipAddress).toByteArray(); String result; try { InetAddress addr = InetAddress.getByAddress(bytes); NetworkInterface netInterface = NetworkInterface.getByInetAddress(addr); Log.d(TAG, "Wifi netInterface.getName() = " + netInterface.getName()); byte[] mac = netInterface.getHardwareAddress(); if (mac == null || mac.length == 0) return ""; StringBuilder buf = new StringBuilder(); for (int idx = 0; idx < mac.length; idx++) { buf.append(String.format("%02X:", mac[idx])); } if (buf.length() > 0) buf.deleteCharAt(buf.length() - 1); return buf.toString(); } catch (UnknownHostException ex) { Log.e(TAG, "getMacAddressFromNetworkInterface() Unknown host.", ex); result = null; } catch (SocketException ex) { Log.e(TAG, "getMacAddressFromNetworkInterface() Socket exception.", ex); result = null; } catch (Exception ex) { Log.e(TAG, "getMacAddressFromNetworkInterface() Exception.", ex); result = null; } return result; }
From source file:com.epam.reportportal.apache.http.impl.conn.TestPoolingHttpClientConnectionManager.java
@Test public void testTargetConnect() throws Exception { final HttpHost target = new HttpHost("somehost", -1, "https"); final InetAddress remote = InetAddress.getByAddress(new byte[] { 10, 0, 0, 1 }); final InetAddress local = InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 }); final HttpRoute route = new HttpRoute(target, local, true); final CPoolEntry entry = new CPoolEntry(LogFactory.getLog(getClass()), "id", route, conn, -1, TimeUnit.MILLISECONDS); entry.markRouteComplete();/*from ww w .j av a2 s.c o m*/ Mockito.when(future.isCancelled()).thenReturn(Boolean.FALSE); Mockito.when(conn.isOpen()).thenReturn(true); Mockito.when(future.isCancelled()).thenReturn(false); Mockito.when(future.get(1, TimeUnit.SECONDS)).thenReturn(entry); Mockito.when(pool.lease(route, null, null)).thenReturn(future); final ConnectionRequest connRequest1 = mgr.requestConnection(route, null); final HttpClientConnection conn1 = connRequest1.get(1, TimeUnit.SECONDS); Assert.assertNotNull(conn1); final HttpClientContext context = HttpClientContext.create(); final SocketConfig sconfig = SocketConfig.custom().build(); mgr.setDefaultSocketConfig(sconfig); Mockito.when(dnsResolver.resolve("somehost")).thenReturn(new InetAddress[] { remote }); Mockito.when(schemePortResolver.resolve(target)).thenReturn(8443); Mockito.when(socketFactoryRegistry.lookup("https")).thenReturn(plainSocketFactory); Mockito.when(plainSocketFactory.createSocket(Mockito.<HttpContext>any())).thenReturn(socket); Mockito.when(plainSocketFactory.connectSocket(Mockito.anyInt(), Mockito.eq(socket), Mockito.<HttpHost>any(), Mockito.<InetSocketAddress>any(), Mockito.<InetSocketAddress>any(), Mockito.<HttpContext>any())) .thenReturn(socket); mgr.connect(conn1, route, 123, context); Mockito.verify(dnsResolver, Mockito.times(1)).resolve("somehost"); Mockito.verify(schemePortResolver, Mockito.times(1)).resolve(target); Mockito.verify(plainSocketFactory, Mockito.times(1)).createSocket(context); Mockito.verify(plainSocketFactory, Mockito.times(1)).connectSocket(123, socket, target, new InetSocketAddress(remote, 8443), new InetSocketAddress(local, 0), context); mgr.routeComplete(conn1, route, context); }
From source file:de.jaetzold.networking.SimpleServiceDiscovery.java
private InetAddress getAndroidLocalIP() { String ipv4 = getIPAddress(true); StringTokenizer tk = new StringTokenizer(ipv4, "."); int a = 0;/*from www. ja va 2 s . c o m*/ int b = 0; int c = 0; int d = 0; try { a = Integer.parseInt(tk.nextToken()); b = Integer.parseInt(tk.nextToken()); c = Integer.parseInt(tk.nextToken()); d = Integer.parseInt(tk.nextToken()); } catch (Exception e) { try { return InetAddress.getByAddress(new byte[] { (byte) a, (byte) b, (byte) c, (byte) d }); } catch (UnknownHostException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } try { return InetAddress.getByAddress(new byte[] { (byte) a, (byte) b, (byte) c, (byte) d }); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }
From source file:org.javamrt.mrt.BGPFileReader.java
private MRTRecord parseBgp4mp(int subtype) throws Exception { // System.out.println("parseBgp4mp("+MRTConstants.mpSubType(subtype)+")"); switch (subtype) { case MRTConstants.BGP4MP_MESSAGE: case MRTConstants.BGP4MP_MESSAGE_AS4: return parseBgp4Update((subtype == MRTConstants.BGP4MP_MESSAGE) ? 2 : 4); /*/* www.ja v a 2 s .c o m*/ * TODO * TTOODDOO:::: * * * case BGP4MP_SNAPSHOT: return new Bgp4mpSnapshot(header,record); */ case MRTConstants.BGP4MP_ENTRY: return parseBgp4Entry(RecordAccess.getU16(record, 6)); case MRTConstants.BGP4MP_STATE_CHANGE: { /* * 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 * 9 0 1 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * | Peer AS number | Local AS number | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * | Interface Index | Address Family | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * | Peer IP address (variable) | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * | Local IP address (variable) | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * | Old State | New State | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ */ int afi = RecordAccess.getU16(record, 6); int addrOffs = 8; int addrSize = (afi == MRTConstants.AFI_IPv4) ? 4 : 16; int stateOffs = addrOffs + 2 * addrSize; int oldState = RecordAccess.getU16(record, stateOffs); stateOffs += 2; int newState = RecordAccess.getU16(record, stateOffs); return new StateChange(RecordAccess.getU32(header, 0), InetAddress.getByAddress(RecordAccess.getBytes(record, addrOffs, addrSize)), new AS(RecordAccess.getU16(record, 0)), oldState, newState); } case MRTConstants.BGP4MP_STATE_CHANGE_AS4: { /* * draft-ietf-grow-mrt-07.txt * * 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 * 9 0 1 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * | Peer AS number | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * | Local AS number | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * | Interface Index | Address Family | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * | Peer IP address (variable) | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * | Local IP address (variable) | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * | Old State | New State | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ */ int afi = RecordAccess.getU16(record, 10); int addrSize = (afi == MRTConstants.AFI_IPv4) ? 4 : 16; int addrOffs = 12; int stateOffs = addrOffs + 2 * addrSize; int oldState = RecordAccess.getU16(record, stateOffs); stateOffs += 2; int newState = RecordAccess.getU16(record, stateOffs); return new StateChange(RecordAccess.getU32(header, 0), InetAddress.getByAddress(RecordAccess.getBytes(record, addrOffs, addrSize)), new AS(RecordAccess.getU32(record, 0)), oldState, newState); } default: break; } MRTRecord result = new MRTRecord(); result.setGeneric(header, record); return result; }
From source file:org.nebulaframework.discovery.multicast.MulticastDiscovery.java
/** * Support routine which processes a response received for a discovery * request for peer clusters. Each invocation will be handled * in a separate thread./* w w w. j av a2 s . c o m*/ * * @param data response data */ private static void processPeerResponse(final byte[] data) { new Thread(new Runnable() { @Override public void run() { try { byte[] ipBytes = Arrays.copyOfRange(data, 0, 4); byte[] portBytes = Arrays.copyOfRange(data, 4, 9); InetAddress ip = null; ip = InetAddress.getByAddress(ipBytes); StringBuilder sb = new StringBuilder(ip.getHostAddress()); sb.append(":"); for (byte b : portBytes) { sb.append(b); } // Add Peer Cluster ClusterManager.getInstance().getPeerService().addCluster(sb.toString()); } catch (Exception e) { log.warn("[Discovery] Unable to resolve peer"); } } }).start(); }
From source file:com.streamsets.pipeline.stage.processor.geolocation.GeolocationProcessor.java
@VisibleForTesting InetAddress toAddress(Field field) throws UnknownHostException, OnRecordErrorException { switch (field.getType()) { case LONG:/* www . j a v a 2s. c om*/ case INTEGER: return InetAddress.getByAddress(ipAsIntToBytes(field.getValueAsInteger())); case STRING: String ip = field.getValueAsString(); if (ip != null) { ip = ip.trim(); if (ip.contains(".")) { return InetAddress.getByAddress(ipAsStringToBytes(ip)); } else { try { return InetAddress.getByAddress(ipAsIntToBytes(Integer.parseInt(ip))); } catch (NumberFormatException nfe) { throw new OnRecordErrorException(Errors.GEOIP_06, ip, nfe); } } } else { throw new OnRecordErrorException(Errors.GEOIP_06, ip); } default: throw new IllegalStateException(Utils.format("Unknown field type: ", field.getType())); } }