List of usage examples for java.net InetAddress getByAddress
public static InetAddress getByAddress(byte[] addr) throws UnknownHostException
From source file:org.peterbaldwin.vlcremote.sweep.Worker.java
@Override public void run() { // Note: InetAddress#isReachable(int) always returns false for Windows // hosts because applications do not have permission to perform ICMP // echo requests. for (;;) {//from w w w . j av a2 s. com byte[] ipAddress = mManager.pollIpAddress(); if (ipAddress == null) { break; } try { InetAddress address = InetAddress.getByAddress(ipAddress); String hostAddress = address.getHostAddress(); URL url = createUrl("http", hostAddress, mPort, mPath); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(1000); try { int responseCode = connection.getResponseCode(); String responseMessage = connection.getResponseMessage(); InputStream inputStream = (responseCode == HttpURLConnection.HTTP_OK) ? connection.getInputStream() : connection.getErrorStream(); if (inputStream != null) { try { int length = connection.getContentLength(); InputStreamEntity entity = new InputStreamEntity(inputStream, length); entity.setContentType(connection.getContentType()); // Copy the entire response body into memory // before the HTTP connection is closed: String body = EntityUtils.toString(entity); ProtocolVersion version = new ProtocolVersion("HTTP", 1, 1); String hostname = address.getHostName(); StatusLine statusLine = new BasicStatusLine(version, responseCode, responseMessage); HttpResponse response = new BasicHttpResponse(statusLine); response.setHeader(HTTP.TARGET_HOST, hostname + ":" + mPort); response.setEntity(new StringEntity(body)); mCallback.onReachable(ipAddress, response); } finally { inputStream.close(); } } else { Log.w(TAG, "InputStream is null"); } } finally { connection.disconnect(); } } catch (IOException e) { mCallback.onUnreachable(ipAddress, e); } } }
From source file:com.eincs.athens.core.Analyzers.java
/** * travel all analyzers and invoke analyze method * @param request request information of analyze * @return report of analyze/*from w ww . j a v a 2s . co m*/ */ public AthensReport invokeAnalyzers(AthensRequest request) { // invoke analyzers AnalyzeResult result = AnalyzeResult.create(AnalyzeResultType.PANALTY); synchronized (this) { for (AnalyzerHodler holder : analyzerHolders) { try { AnalyzeResult newResult = holder.analyzer.analyze(request); result.merge(newResult); } catch (Exception e) { logger.error(e.getMessage(), e); } } } // create report with result and request AthensReport report = new AthensReport(); report.setRequestSeq(request.getRequestSeq()); report.setTargetKey(request.getTargetKey()); report.setResult(result); report.setTags(request.getTags()); if (report.needNotify()) { try { String ipAddr = InetAddress.getByAddress(request.getTargetKey().getAddress()).toString(); if (StringUtils.startsWith(ipAddr, "/")) { ipAddr = ipAddr.substring(1); } MysqlHandler.insert(ipAddr, request.getTargetKey().getMethod(), request.getTargetKey().getPath(), RateLimitAnalyzer.class.getSimpleName(), System.currentTimeMillis() + result.getPanalty()); } catch (UnknownHostException e) { logger.error(e.getMessage(), e); } } return report; }
From source file:net.fenyo.gnetwatch.GenericTools.java
/** * Converts an IP address to its string representation. * @param addr ipv6 address./*from w w w . j ava 2 s . co m*/ * @return String ascii representation. */ static public String inet6AddressToString(final Inet6Address addr) { try { byte bytes[] = addr.getAddress(); return InetAddress.getByAddress(bytes).toString().substring(1); } catch (final UnknownHostException ex) { log.error("Exception", ex); } return ""; }
From source file:nl.sidn.pcap.util.GeoLookupUtil.java
public String lookupCountry(byte[] ip) { try {//from www. j a va2s .c o m return lookupCountry(InetAddress.getByAddress(ip)); } catch (UnknownHostException e) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("No country found for: " + ip); } return null; } }
From source file:github.daneren2005.serverproxy.ServerProxy.java
public String getPublicAddress(String request) { WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); int ipAddress = wifiManager.getConnectionInfo().getIpAddress(); if (ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) { ipAddress = Integer.reverseBytes(ipAddress); }/* w w w .j a v a2s . co m*/ byte[] ipByteArray = BigInteger.valueOf(ipAddress).toByteArray(); String ipAddressString = null; try { ipAddressString = InetAddress.getByAddress(ipByteArray).getHostAddress(); } catch (UnknownHostException ex) { Log.e(TAG, "Unable to get host address."); } return getAddress(ipAddressString, request); }
From source file:io.mandrel.OtherTest.java
@Test @SneakyThrows//from w ww . j a va 2 s . co m public void whut4() { String add = "208.67.220.220:53"; Splitter splitter = Splitter.on(":"); List<String> split = Lists.newArrayList(splitter.split(add)); InetAddresses.forString(split.get(0)); new InetSocketAddress(InetAddress.getByAddress(split.get(0).getBytes(Charsets.UTF_8)), split.size() == 2 ? Integer.valueOf(split.get(1)) : 53); }
From source file:com.offbynull.portmapper.natpmp.ExternalAddressNatPmpResponse.java
/** * Constructs a {@link ExternalAddressNatPmpResponse} object by parsing a buffer. * @param buffer buffer containing PCP response data * @throws NullPointerException if any argument is {@code null} * @throws BufferUnderflowException if not enough data is available in {@code buffer} * @throws IllegalArgumentException if the version doesn't match the expected version (must always be {@code 0}), or if the op * {@code != 128}, or if there's an unsuccessful/unrecognized result code */// w w w .j a v a 2 s . c o m public ExternalAddressNatPmpResponse(ByteBuffer buffer) { super(buffer); Validate.isTrue(getOp() == 128); byte[] addr = new byte[4]; buffer.get(addr); try { address = InetAddress.getByAddress(addr); } catch (UnknownHostException uhe) { throw new IllegalStateException(uhe); // should never happen, will always be 4 bytes } }
From source file:org.opennms.netmgt.snmp.snmp4j.Snmp4JValue.java
Snmp4JValue(final int syntax, final byte[] bytes) { switch (syntax) { case SMIConstants.SYNTAX_INTEGER: { m_value = new Integer32(new BigInteger(bytes).intValue()); break;/*from w w w .ja v a2 s . com*/ } case SMIConstants.SYNTAX_COUNTER32: { m_value = new Counter32(new BigInteger(bytes).longValue()); break; } case SMIConstants.SYNTAX_COUNTER64: { m_value = new Counter64(new BigInteger(bytes).longValue()); break; } case SMIConstants.SYNTAX_TIMETICKS: { m_value = new TimeTicks(new BigInteger(bytes).longValue()); break; } case SMIConstants.SYNTAX_UNSIGNED_INTEGER32: { m_value = new UnsignedInteger32(new BigInteger(bytes).longValue()); break; } case SMIConstants.SYNTAX_IPADDRESS: { try { m_value = new IpAddress(InetAddress.getByAddress(bytes)); } catch (final UnknownHostException e) { throw new IllegalArgumentException("unable to create InetAddress from bytes: " + e.getMessage()); } break; } case SMIConstants.SYNTAX_OBJECT_IDENTIFIER: { m_value = new OID(new String(bytes)); break; } case SMIConstants.SYNTAX_OCTET_STRING: { m_value = new OctetString(bytes); break; } case SMIConstants.SYNTAX_OPAQUE: { m_value = new Opaque(bytes); break; } case SMIConstants.EXCEPTION_END_OF_MIB_VIEW: { m_value = Null.endOfMibView; break; } case SMIConstants.EXCEPTION_NO_SUCH_INSTANCE: { m_value = Null.noSuchInstance; break; } case SMIConstants.EXCEPTION_NO_SUCH_OBJECT: { m_value = Null.noSuchObject; break; } case SMIConstants.SYNTAX_NULL: { m_value = new Null(); break; } default: throw new IllegalArgumentException("invalid syntax " + syntax); } if (m_value == null) { throw new IllegalArgumentException("value object created from syntax " + syntax + " is null"); } }
From source file:org.kontalk.Kontalk.java
private Kontalk(String[] args) { // check if already running try {/* ww w . java 2 s. c o m*/ InetAddress addr = InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 }); RUN_LOCK = new ServerSocket(9871, 10, addr); } catch (java.net.BindException ex) { LOGGER.severe("already running"); System.exit(2); } catch (IOException ex) { LOGGER.log(Level.WARNING, "can't create socket", ex); } this.parseArgs(args); }
From source file:com.xsdn.main.util.IpNetwork.java
/** * Create an {@link InetAddress} instance which represents the IP address * specified by the given byte array.//from w ww.j a v a 2 s.co m * * @param bytes A byte array which represents the IP address. * @return An {@link InetAddress} instance. * @throws IllegalArgumentException * The given byte array does not represent an IP network mask. */ public static final InetAddress getInetAddress(byte[] bytes) { try { return InetAddress.getByAddress(bytes); } catch (Exception e) { throw new IllegalArgumentException("Invalid raw IP address: " + ByteUtils.toHexString(bytes), e); } }