List of usage examples for java.net InetAddress getByAddress
public static InetAddress getByAddress(byte[] addr) throws UnknownHostException
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();/*from w w w.ja va 2 s . c o m*/ 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
/** * Returns the wi-fi broadcast address/*w w w. j a va 2s . co m*/ * * @param context the app context * @return returns an the broadcast address * @throws IOException */ public static InetAddress getBroadcastAddress(Context context) throws IOException { WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); DhcpInfo dhcp = wifi.getDhcpInfo(); // handle null somehow 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:org.yestech.lib.net.InternetAddress.java
public static InetAddress createIp4Address(String address) { String[] tempOctects = StringUtils.split(address, "."); if (tempOctects.length == 4) { byte[] octets = new byte[4]; for (int i = 0; i < tempOctects.length; i++) { String tempOctect = tempOctects[i]; octets[i] = Byte.valueOf(tempOctect); }//w ww . j a va 2 s . c om try { return InetAddress.getByAddress(octets); } catch (UnknownHostException e) { throw new RuntimeException(e); } } else { throw new IllegalArgumentException("not a valid ip4 address"); } }
From source file:Main.java
public static InetAddress intToInet(int value) { byte[] bytes = new byte[4]; for (int i = 0; i < 4; i++) { bytes[i] = byteOfInt(value, i);//from w w w . j av a2 s . c o m } try { return InetAddress.getByAddress(bytes); } catch (UnknownHostException e) { // This only happens if the byte array has a bad length return null; } }
From source file:Main.java
/** * Calculates the network mask address from the value * //from w w w.ja v a 2s.c o m * @param numericMask * the network mask * @return */ public static String networkMaskFromInt(int numericMask) { // StringBuffer sb = new StringBuffer(15); // for (int shift = 24; shift > 0; shift -= 8) { // // process 3 bytes, from high order byte down. // sb.append(Integer.toString((numericMask >>> shift) & 0xff)); // sb.append('.'); // } // sb.append(Integer.toString(numericMask & 0xff)); // return sb.toString(); // } // // public String getMaskAsString() { int value = 0xffffffff << (32 - numericMask); byte[] bytes = new byte[] { (byte) (value >>> 24), (byte) (value >> 16 & 0xff), (byte) (value >> 8 & 0xff), (byte) (value & 0xff) }; InetAddress netAddr = null; try { netAddr = InetAddress.getByAddress(bytes); } catch (UnknownHostException e) { e.printStackTrace(); } return netAddr.getHostAddress(); }
From source file:Main.java
public static InetAddress getIpAddress(Context context) throws UnknownHostException { WifiManager wifiMgr = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = wifiMgr.getConnectionInfo(); int ip = wifiInfo.getIpAddress(); if (ip == 0) { return null; } else {//ww w . j a v a 2s. co m byte[] ipAddress = convertIpAddress(ip); return InetAddress.getByAddress(ipAddress); } }
From source file:Main.java
protected static InetSocketAddress deserialiseAddress(DataInputStream is) throws IOException { byte[] bytes = deserialiseByteArray(is, 16); int port = is.readShort() & 0xffff; return (new InetSocketAddress(InetAddress.getByAddress(bytes), port)); }
From source file:Main.java
static InetAddress parseLinuxSubnetMask(String line) throws UnknownHostException { int e = line.indexOf("broadcast"); if (e == -1)/* w ww.jav a2s . co m*/ return null; e--; String v = line.substring(line.indexOf("netmask") + 8, e); if (v.startsWith("0x")) { try { long address = Long.parseLong(v.substring(2), 16); return InetAddress.getByAddress(new byte[] { (byte) (address >> 24), (byte) ((address >> 16) & 0xff), (byte) ((address >> 8) & 0xff), (byte) (address & 0xff) }); } catch (NumberFormatException e2) { } } else { return InetAddress.getByName(v); } return null; }
From source file:NetUtil.java
/** * Resolves ip address and returns host name as a string. *///from w w w . ja v a2 s .c om public static String resolveIp(byte[] ip) { try { InetAddress addr = InetAddress.getByAddress(ip); return addr.getHostName(); } catch (UnknownHostException uhex) { return null; } }
From source file:org.apache.axis2.transport.testkit.util.ServerUtil.java
/** * Wait until the server listening on a given TCP port is ready to accept * connections./*from ww w . j a va 2 s. c o m*/ * * @param port The TCP port the server listens on. * @throws Exception */ public static void waitForServer(int port) throws Exception { InetAddress localhost = InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 }); int attempts = 0; Socket socket = null; while (socket == null) { attempts++; try { socket = new Socket(localhost, port); } catch (ConnectException ex) { if (attempts < 10) { Thread.sleep(50); } else { throw ex; } } } log.debug("Server on port " + port + " ready after " + attempts + " connection attempts"); socket.close(); }