List of usage examples for java.net InetAddress getHostAddress
public String getHostAddress()
From source file:de.flapdoodle.embed.redis.RedisDProcess.java
public static boolean shutdownRedis(AbstractRedisConfig config) { try {/*w w w. ja v a 2 s . com*/ // ensure that we don't get into a stackoverflow when starting // the artifact fails entirely if (config.isNested()) { logger.log(Level.INFO, "Nested stop, won't execute redis process again"); return false; } InetAddress host = config.net().getServerAddress(); int port = config.net().getPort(); if (!host.isLoopbackAddress()) { logger.log(Level.WARNING, "" + "---------------------------------------\n" + "Your localhost (" + host.getHostAddress() + ") is not a loopback adress\n" + "We can NOT send shutdown to redis, because it is denied from remote." + "---------------------------------------\n"); return false; } try { Jedis j = new Jedis(host.getHostName(), port); String reply = j.shutdown(); if (StringUtils.isEmpty(reply)) { return true; } else { logger.log(Level.SEVERE, String .format("sendShutdown closing %s:%s; Got response from server %s", host, port, reply)); return false; } } catch (JedisConnectionException e) { logger.log(Level.WARNING, String.format("sendShutdown closing %s:%s. No Service listening on address.\n%s", host, port, e.getMessage())); return true; } } catch (UnknownHostException e) { logger.log(Level.SEVERE, "sendStop", e); } return false; }
From source file:be.deadba.ampd.SettingsActivity.java
private static String getLocalIpAddress() { try {/*from ww w. j a va2s .c o 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(); String hostAddress = inetAddress.getHostAddress(); if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(hostAddress)) { return hostAddress; } } } } catch (Exception ex) { Log.e(TAG, ex.toString()); } return null; }
From source file:net.grinder.util.NetworkUtils.java
/** * Get local address by connecting to a server. * * @param byConnecting the server address to connect. * @param port the port to connect * @return IP address local IP address/*from ww w.j a v a 2 s. co m*/ */ static String getLocalHostAddress(String byConnecting, int port) { InetAddress addr = getLocalInetAddress(byConnecting, port); if (addr != null) { return addr.getHostAddress(); } else { // It's final... return "127.0.0.1"; } }
From source file:ws.argo.DemoWebClient.Browser.BrowserController.java
private static String getHostIPAddressFromString(String propIPAddress, String niName) { String hostIPAddr = propIPAddress; if (propIPAddress.equals("")) { // If the listenerIPAddress is blank, then try to get the ip address of // the interface try {/*from w w w . j a v a2 s. co m*/ NetworkInterface ni = null; if (niName != null) ni = NetworkInterface.getByName(niName); if (ni == null) { LOGGER.info("Network Interface name not specified or incorrect. Defaulting to localhost"); hostIPAddr = InetAddress.getLocalHost().getHostAddress(); } else { if (!ni.isLoopback()) { Enumeration<InetAddress> ee = ni.getInetAddresses(); while (ee.hasMoreElements()) { InetAddress i = (InetAddress) ee.nextElement(); if (i instanceof Inet4Address) { hostIPAddr = i.getHostAddress(); break; // get the first one an get out of the loop } } } } } catch (SocketException e) { LOGGER.warn("A socket exception occurred."); } catch (UnknownHostException e) { LOGGER.warn("Error finding Network Interface", e); } } return hostIPAddr; }
From source file:com.git.original.common.utils.IPUtils.java
/** * ?IP?/*from ww w. j av a 2 s. c om*/ * <p> * :<br/> * 1. 192.xxx.xxx.xxx<br> * 2. 172.xxx.xxx.xxx<br> * 3. 10.xxx.xxx.xxx<br> * other<br> * * @return ipv4? * @throws SocketException */ private static synchronized int doGetLocalIp() throws SocketException { if (localIp != 0) { return localIp; } Integer ipStartWith10 = null; Integer ipStartWith172 = null; Integer other = null; /* * ?IP? */ Enumeration<NetworkInterface> interfaceEnum = NetworkInterface.getNetworkInterfaces(); while (interfaceEnum.hasMoreElements()) { NetworkInterface netInterface = interfaceEnum.nextElement(); if (!netInterface.isUp()) { continue; } Enumeration<InetAddress> addrEnum = netInterface.getInetAddresses(); while (addrEnum.hasMoreElements()) { InetAddress addr = addrEnum.nextElement(); String hostAddr = addr.getHostAddress(); if (hostAddr.startsWith("192.")) { localIp = ByteUtils.toInt(addr.getAddress()); return localIp; } else if (ipStartWith172 == null && hostAddr.startsWith("172.")) { ipStartWith172 = ByteUtils.toInt(addr.getAddress()); } else if (ipStartWith10 == null && hostAddr.startsWith("10.")) { ipStartWith10 = ByteUtils.toInt(addr.getAddress()); } else if (other == null && (addr instanceof Inet4Address)) { other = ByteUtils.toInt(addr.getAddress()); } } } if (ipStartWith172 != null) { localIp = ipStartWith172; return localIp; } else if (ipStartWith10 != null) { localIp = ipStartWith10; return localIp; } else if (other != null) { localIp = other; return localIp; } throw new RuntimeException("can not get Local Server IPv4 Address"); }
From source file:com.git.original.common.utils.IPUtils.java
/** * ?IP?//from www . j a va2 s . c o m * <p> * :<br/> * 1. 220.xxx.xxx.xxx<br> * 2. 123.xxx.xxx.xxx<br> * other<br> * * @return * @throws SocketException * If an I/O error occurs. * @throws NullPointerException * can not found the interface * @throws RuntimeException * can not get net address */ public static InetAddress getWANIpv4Address(String interfaceName) throws SocketException, NullPointerException, RuntimeException { InetAddress ipStartWith123 = null; InetAddress ipv4Addr = null; if (StringUtils.isNotEmpty(interfaceName)) { // ? NetworkInterface netInterface = NetworkInterface.getByName(interfaceName.trim()); if (netInterface == null) { throw new NullPointerException("can not found the network interface by name: " + interfaceName); } Enumeration<InetAddress> addrEnum = netInterface.getInetAddresses(); while (addrEnum.hasMoreElements()) { InetAddress addr = addrEnum.nextElement(); String hostAddr = addr.getHostAddress(); if (hostAddr.startsWith("220.")) { return addr; } else if (ipStartWith123 == null && hostAddr.startsWith("123.")) { ipStartWith123 = addr; } else if (addr instanceof Inet4Address) { ipv4Addr = addr; } } } else { /* * ??? */ Enumeration<NetworkInterface> interfaceEnum = NetworkInterface.getNetworkInterfaces(); while (interfaceEnum.hasMoreElements()) { NetworkInterface netInterface = interfaceEnum.nextElement(); if (netInterface.isLoopback() || !netInterface.isUp()) { continue; } Enumeration<InetAddress> addrEnum = netInterface.getInetAddresses(); while (addrEnum.hasMoreElements()) { InetAddress addr = addrEnum.nextElement(); String hostAddr = addr.getHostAddress(); if (hostAddr.startsWith("220.")) { return addr; } else if (ipStartWith123 == null && hostAddr.startsWith("123.")) { ipStartWith123 = addr; } else if (addr instanceof Inet4Address) { ipv4Addr = addr; } } } } if (ipStartWith123 != null) { return ipStartWith123; } else if (ipv4Addr != null) { return ipv4Addr; } throw new RuntimeException("can not get WAN Address"); }
From source file:com.bigdata.dastor.db.HintedHandOffManager.java
private static boolean sendMessage(InetAddress endPoint, String tableName, String cfName, String key) throws IOException { if (!Gossiper.instance.isKnownEndpoint(endPoint)) { logger_.warn("Hinted handoff found for endpoint " + endPoint.getHostAddress() + " which is not part of the gossip network. discarding."); return true; }// www .j av a2 s . c o m if (!FailureDetector.instance.isAlive(endPoint)) { logger_.warn("Hinted handoff found for endpoint " + endPoint.getHostAddress() + " which is not alive. stopping."); return false; } Table table = Table.open(tableName); ColumnFamilyStore cfs = table.getColumnFamilyStore(cfName); byte[] startColumn = ArrayUtils.EMPTY_BYTE_ARRAY; while (true) { QueryFilter filter = new SliceQueryFilter(key, new QueryPath(cfs.getColumnFamilyName()), startColumn, ArrayUtils.EMPTY_BYTE_ARRAY, false, PAGE_SIZE); ColumnFamily cf = cfs.getColumnFamily(filter); if (pagingFinished(cf, startColumn)) break; if (cf.getColumnNames().isEmpty()) { if (logger_.isDebugEnabled()) logger_.debug("Nothing to hand off for " + key); break; } startColumn = cf.getColumnNames().last(); RowMutation rm = new RowMutation(tableName, key); rm.add(cf); Message message = rm.makeRowMutationMessage(); WriteResponseHandler responseHandler = new WriteResponseHandler(1, tableName); // BIGDATA: retry int tryNum = 0; for (tryNum = 0; tryNum < 3; tryNum++) { MessagingService.instance.sendRR(message, new InetAddress[] { endPoint }, responseHandler); try { responseHandler.get(); break; } catch (TimeoutException e) { } } if (tryNum >= 3) { logger_.warn("Hinted handoff found for endpoint " + endPoint.getHostAddress() + " send message TimeoutException."); return false; } } return true; }
From source file:com.asquareb.kaaval.MachineKey.java
/** * Method to decrypt a string. Accepts the string to be decrypted and the * name of the file which stores the key values */// w w w. ja va2 s.c o m public static String decrypt(String property, String app) throws IOException, KaavalException { SecretKey key = null; ObjectInputStream is = null; MachineKey mKey = null; int eti = 0; Cipher pbeCipher = null; InetAddress ip = InetAddress.getLocalHost(); NetworkInterface macAddress = NetworkInterface.getByInetAddress(ip); byte[] macId = macAddress.getHardwareAddress(); try { is = new ObjectInputStream(new BufferedInputStream(new FileInputStream(app))); mKey = (MachineKey) is.readObject(); key = mKey.yek; salt = mKey.tlas; eti = mKey.eti; String ipa = ip.getHostAddress(); if (!ipa.equals(mKey.api) || !new String(macId).equals(mKey.macad)) throw new KaavalException(5, "Key file is not for this machine"); is.close(); pbeCipher = Cipher.getInstance("PBEWithMD5AndDES"); pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(salt, eti)); return new String(pbeCipher.doFinal(base64Decode(property))); } catch (IOException e) { throw new KaavalException(3, "Error in reading key file during decryption", e); } catch (KaavalException e) { throw e; } catch (Exception e) { throw new KaavalException(4, "Error during decryption", e); } finally { if (is != null) is.close(); } }
From source file:comikit.droidscript.DroidScriptServer.java
public static String[] getIpAddresses() { try {/*from ww w. j a v a 2s .c o m*/ List<String> ipaddresses = new ArrayList<String>(); Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface interf = interfaces.nextElement(); Enumeration<InetAddress> adresses = interf.getInetAddresses(); while (adresses.hasMoreElements()) { InetAddress address = adresses.nextElement(); if (!address.isLoopbackAddress()) { ipaddresses.add(address.getHostAddress().toString()); } } } if (0 < ipaddresses.size()) { return ipaddresses.toArray(new String[1]); } } catch (SocketException e) { e.printStackTrace(); } return null; }
From source file:com.bjond.utilities.MiscUtils.java
/** * Returns the host string. If it's on Openshift, first check for the 'OPENSHIFT_PUBLIC_URL' * which is something we set for our known instances (test, alpha, etc). If that isn't there, get * the default variable RedHat sets for the generated application URL. If that isn't there, get * the ip address of the running server. This is usually for development; remember to make your * server publicly available for this to work. By default Wildfly is configured to only listen on * localhost./* w w w .j a v a 2s .c o m*/ * * If everything fails, just assume we're running on localhost. * * @return The hostname. */ static public String getHostString() { String host = "http://localhost:8080"; if (System.getenv("OPENSHIFT_PUBLIC_URL") != null) { host = System.getenv("OPENSHIFT_PUBLIC_URL"); } else if (System.getenv("OPENSHIFT_APP_DNS") != null) { host = "http://" + System.getenv("OPENSHIFT_APP_DNS"); } else { try { InetAddress local = InetAddress.getLocalHost(); host = "http://" + local.getHostAddress() + ":8080"; } catch (Exception ex) { log.error(ex.getMessage()); } } return host; }