List of usage examples for java.net InetAddress getHostAddress
public String getHostAddress()
From source file:impl.underdark.transport.nsd.BnjUtil.java
public static InetAddress getLocalIpAddress() { try {/*w w w . j a v a 2 s . co 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(); if (inetAddress.isLoopbackAddress()) continue; if (!InetAddressUtils.isIPv4Address(inetAddress.getHostAddress())) continue; //if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress() && inetAddress.isSiteLocalAddress() ) { //if (!inetAddress.isLoopbackAddress() // && InetAddressUtils.isIPv4Address(inetAddress.getHostAddress()) ) return inetAddress; } // for } // for } catch (SocketException ex) { Logger.error("nsd server failed to get local address"); } return null; }
From source file:org.apache.synapse.transport.nhttp.util.NhttpUtil.java
/** * This method tries to determine the hostname of the given InetAddress without * triggering a reverse DNS lookup. {@link java.net.InetAddress#getHostName()} * triggers a reverse DNS lookup which can be very costly in cases where reverse * DNS fails. Tries to parse a symbolic hostname from {@link java.net.InetAddress#toString()}, * which is documented to return a String of the form "hostname / literal IP address" * with 'hostname' blank if not already computed & stored in <code>address</code<. * <p/>/*ww w . j av a 2s . com*/ * If the hostname cannot be determined from InetAddress.toString(), * the value of {@link java.net.InetAddress#getHostAddress()} is returned. * * @param address The InetAddress whose hostname has to be determined * @return hostsname, if it can be determined. hostaddress, if not. * * TODO: We may introduce a System property or some other method of configuration * TODO: which will specify whether to allow reverse DNS lookup or not */ public static String getHostName(InetAddress address) { String result; String hostAddress = address.getHostAddress(); String inetAddr = address.toString(); int index1 = inetAddr.lastIndexOf('/'); int index2 = inetAddr.indexOf(hostAddress); if (index2 == index1 + 1) { if (index1 == 0) { result = hostAddress; } else { result = inetAddr.substring(0, index1); } } else { result = hostAddress; } return result; }
From source file:com.splout.db.common.SploutConfiguration.java
/** * Get the Splout configuration using double configuration: defaults + custom *//* w w w . j a v a 2 s. c o m*/ public static SploutConfiguration get(String rootDir) { SploutConfiguration properties = new SploutConfiguration(); PropertiesConfiguration config = load(rootDir, SPLOUT_PROPERTIES, false); if (config != null) { properties.addConfiguration(config); } config = load(rootDir, SPLOUT_PROPERTIES + ".default", true); properties.addConfiguration(config); // The following lines replaces the default "localhost" by the local IP for convenience: String myIp = "localhost"; try { Collection<InetAddress> iNetAddresses = GetIPAddresses.getAllLocalIPs(); // but only if there is Internet connectivity! if (iNetAddresses != null) { Iterator<InetAddress> it = iNetAddresses.iterator(); if (it.hasNext()) { InetAddress address = it.next(); if (address.getHostAddress() != null) { myIp = address.getHostAddress(); } } } } catch (IOException e) { throw new RuntimeException(e); } if (config.getString(QNodeProperties.HOST) != null && config.getString(QNodeProperties.HOST).equals("localhost")) { config.setProperty(QNodeProperties.HOST, myIp); } if (config.getString(DNodeProperties.HOST) != null && config.getString(DNodeProperties.HOST).equals("localhost")) { config.setProperty(DNodeProperties.HOST, myIp); } return properties; }
From source file:Main.java
public static String getIfIpAddress() { String result = ""; try {//from ww w.j a va 2s . 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(); // Log.v("","ip="+inetAddress.getHostAddress()); if (!inetAddress.isLoopbackAddress() && (inetAddress.getHostAddress().startsWith("0") || inetAddress.getHostAddress().startsWith("1") || inetAddress.getHostAddress().startsWith("2"))) { result = inetAddress.getHostAddress(); break; } } } } catch (SocketException ex) { Log.e(DEBUG_TAG, ex.toString()); result = "192.168.0.1"; } // Log.v("","getIfIpAddress result="+result); if (result.equals("")) result = "192.168.0.1"; return result; }
From source file:ntpgraphic.PieChart.java
public static void NTPClient(String[] servers) { Properties defaultProps = System.getProperties(); //obtiene las "properties" del sistema defaultProps.put("java.net.preferIPv6Addresses", "true");//mapea el valor true en la variable java.net.preferIPv6Addresses if (servers.length == 0) { System.err.println("Usage: NTPClient <hostname-or-address-list>"); System.exit(1);//w w w . j a v a 2s. c om } Promedio = 0; Cant = 0; int j = 1; NTPUDPClient client = new NTPUDPClient(); // We want to timeout if a response takes longer than 10 seconds client.setDefaultTimeout(10000); try { client.open(); for (String arg : servers) { System.out.println(); try { InetAddress hostAddr = InetAddress.getByName(arg); System.out.println("> " + hostAddr.getHostName() + "/" + hostAddr.getHostAddress()); TimeInfo info = client.getTime(hostAddr); processResponse(info, j++); } catch (IOException ioe) { System.err.println(ioe.toString()); } } } catch (SocketException e) { System.err.println(e.toString()); } client.close(); //System.out.println("\n Pomedio "+(Promedio/Cant)); }
From source file:com.cosmicpush.pub.push.XmppPush.java
public static XmppPush newPush(String recipient, String message, String callbackUrl, String... traits) { InetAddress remoteAddress = PushUtils.getLocalHost(); return new XmppPush(recipient, message, callbackUrl, remoteAddress.getCanonicalHostName(), remoteAddress.getHostAddress(), BeanUtils.toMap(traits)); }
From source file:Main.java
public static String getServiceIp(String host) { InetAddress myServer = null; try {/*from w w w. j ava 2 s . co m*/ myServer = InetAddress.getByName(host); } catch (UnknownHostException e) { e.printStackTrace(); } if (myServer == null) { return "0"; } return myServer.getHostAddress(); }
From source file:com.cosmicpush.pub.push.XmppPush.java
public static XmppPush newPush(String recipient, String message, String callbackUrl, Map<String, String> traits) { InetAddress remoteAddress = PushUtils.getLocalHost(); return new XmppPush(recipient, message, callbackUrl, remoteAddress.getCanonicalHostName(), remoteAddress.getHostAddress(), traits); }
From source file:dk.netarkivet.common.utils.SystemUtils.java
/** * Get the first hostname available for this machine, or "localhost" if none * are available./*from w w w .j a va2s .co m*/ * * @return A hostname, as returned by * InetAddress.getLocalHost().getCanonicalHostName()() */ public static String getLocalHostName() { String hostname = LOCALHOST; try { InetAddress localhost = InetAddress.getLocalHost(); String localhostName = localhost.getCanonicalHostName(); String localhostIp = localhost.getHostAddress(); if (log.isTraceEnabled()) { log.trace("[getLocalHostName] Resolved: " + localhostName + " (" + localhostIp + ")"); } return localhostName; } catch (UnknownHostException e) { // If no interfaces, use default; log.warn("Unable to resolve localhostname. Returning the default '" + LOCALHOST + "'"); } return hostname; }
From source file:com.jean.farCam.SettingsActivity.java
public static String getIPAddress() { try {/*from ww w. jav a2 s . c o m*/ List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface intf : interfaces) { List<InetAddress> addrs = Collections.list(intf.getInetAddresses()); for (InetAddress addr : addrs) { if (!addr.isLoopbackAddress()) { String sAddr = addr.getHostAddress().toUpperCase(); if (InetAddressUtils.isIPv4Address(sAddr)) { return sAddr; } } } } } catch (Exception ex) { } return ""; }