List of usage examples for java.net InetAddress getHostAddress
public String getHostAddress()
From source file:com.buaa.cfs.net.DNS.java
/** * Returns the hostname associated with the specified IP address by the provided nameserver. * <p>// w w w .java 2 s. co m * Loopback addresses * * @param hostIp The address to reverse lookup * @param ns The host name of a reachable DNS server * * @return The host name associated with the provided IP * * @throws NamingException If a NamingException is encountered */ public static String reverseDns(InetAddress hostIp, String ns) throws NamingException { // // Builds the reverse IP lookup form // This is formed by reversing the IP numbers and appending in-addr.arpa // String[] parts = hostIp.getHostAddress().split("\\."); String reverseIP = parts[3] + "." + parts[2] + "." + parts[1] + "." + parts[0] + ".in-addr.arpa"; DirContext ictx = new InitialDirContext(); Attributes attribute; try { attribute = ictx.getAttributes("dns://" // Use "dns:///" if the default + ((ns == null) ? "" : ns) + // nameserver is to be used "/" + reverseIP, new String[] { "PTR" }); } finally { ictx.close(); } String hostname = attribute.get("PTR").get().toString(); int hostnameLength = hostname.length(); if (hostname.charAt(hostnameLength - 1) == '.') { hostname = hostname.substring(0, hostnameLength - 1); } return hostname; }
From source file:org.apache.axis2.transport.nhttp.ServerWorker.java
/** * Copied from transport.http of Axis2//w ww. java 2s.c o m * * Returns the ip address to be used for the replyto epr * CAUTION: * This will go through all the available network interfaces and will try to return an ip address. * First this will try to get the first IP which is not loopback address (127.0.0.1). If none is found * then this will return this will return 127.0.0.1. * This will <b>not<b> consider IPv6 addresses. * <p/> * TODO: * - Improve this logic to genaralize it a bit more * - Obtain the ip to be used here from the Call API * * @return Returns String. * @throws java.net.SocketException */ private static String getIpAddress() throws SocketException { Enumeration e = NetworkInterface.getNetworkInterfaces(); String address = "127.0.0.1"; while (e.hasMoreElements()) { NetworkInterface netface = (NetworkInterface) e.nextElement(); Enumeration addresses = netface.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress ip = (InetAddress) addresses.nextElement(); if (!ip.isLoopbackAddress() && isIP(ip.getHostAddress())) { return ip.getHostAddress(); } } } return address; }
From source file:de.madvertise.android.sdk.MadUtil.java
/** * Fetch the address of the enabled interface * /*ww w .j a v a 2 s .c om*/ * @return ip address as string */ protected static String getLocalIpAddress() { try { 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()) { return inetAddress.getHostAddress().toString(); } } } } catch (SocketException ex) { if (PRINT_LOG) Log.d(MadUtil.LOG, ex.toString()); } return ""; }
From source file:com.ghgande.j2mod.modbus.utils.TestUtils.java
/** * Returns the first real IP address it finds * * @return Real IP address or null if nothing available *///w w w . j a v a 2s. c om public static String getFirstIp4Address() { // Get all the physical adapters List<NetworkInterface> adapters = getNetworkAdapters(); if (adapters.size() > 0) { for (NetworkInterface adapter : adapters) { // Loop through all the addresses Enumeration<InetAddress> addresses = adapter.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress address = addresses.nextElement(); // Only interested in non-loopback and IPv4 types if (!address.isLoopbackAddress() && address instanceof Inet4Address) { return address.getHostAddress(); } } } } return null; }
From source file:org.basdroid.common.NetworkUtils.java
/** * Get IP address from first non-localhost interface * @param interfaceName eth0, wlan0 or NULL=use first interface * @param useIPv4 true=return ipv4, false=return ipv6 * @return address or empty string/* w w w. ja v a 2 s . c o m*/ */ public static String getIPAddress(String interfaceName, boolean useIPv4) { try { List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface intf : interfaces) { if (interfaceName != null) { if (!intf.getName().equalsIgnoreCase(interfaceName)) continue; } List<InetAddress> inetAddresses = Collections.list(intf.getInetAddresses()); for (InetAddress inetAddr : inetAddresses) { if (!inetAddr.isLoopbackAddress()) { String address = inetAddr.getHostAddress().toUpperCase(); boolean isIPv4 = InetAddressUtils.isIPv4Address(address); if (useIPv4) { if (isIPv4) { return address; } } else { if (!isIPv4) { int delim = address.indexOf('%'); // drop ip6 port suffix return delim < 0 ? address : address.substring(0, delim); } } } } } } catch (Exception ex) { } // for now eat exceptions return ""; }
From source file:org.apache.synapse.transport.passthru.api.PassThroughNHttpGetProcessor.java
/** * Whatever this method returns as the IP is ignored by the actual http/s listener when * its getServiceEPR is invoked. This was originally copied from axis2 * * @return Returns String.//from ww w . j ava 2s . c o m * @throws java.net.SocketException if the socket can not be accessed */ protected static String getIpAddress() throws SocketException { Enumeration e = NetworkInterface.getNetworkInterfaces(); String address = "127.0.0.1"; while (e.hasMoreElements()) { NetworkInterface networkInterface = (NetworkInterface) e.nextElement(); Enumeration addresses = networkInterface.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress ip = (InetAddress) addresses.nextElement(); if (!ip.isLoopbackAddress() && isIP(ip.getHostAddress())) { return ip.getHostAddress(); } } } return address; }
From source file:net.centro.rtb.monitoringcenter.infos.NodeInfo.java
private static String detectPublicIpAddress() { Enumeration<NetworkInterface> networkInterfaces = null; try {//w w w .java 2 s.c om networkInterfaces = NetworkInterface.getNetworkInterfaces(); } catch (SocketException e) { logger.debug("Unable to obtain network interfaces!", e); return null; } for (NetworkInterface networkInterface : Collections.list(networkInterfaces)) { boolean isLoopback = false; try { isLoopback = networkInterface.isLoopback(); } catch (SocketException e) { logger.debug("Unable to identify if a network interface is a loopback or not"); } if (!isLoopback) { Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses(); while (inetAddresses.hasMoreElements()) { InetAddress inetAddress = inetAddresses.nextElement(); if (Inet4Address.class.isInstance(inetAddress)) { if (!inetAddress.isLoopbackAddress() && !inetAddress.isAnyLocalAddress() && !inetAddress.isLinkLocalAddress() && !inetAddress.isSiteLocalAddress()) { return inetAddress.getHostAddress(); } } } } } return null; }
From source file:com.alibaba.jstorm.utils.NetWorkUtils.java
public static List<String> host2Ip(List<String> servers) { if (servers == null || servers.size() == 0) { return new ArrayList<String>(); }//www . ja v a 2 s . c o m Set<String> ret = new HashSet<String>(); for (String server : servers) { if (StringUtils.isBlank(server)) { continue; } InetAddress ia; try { ia = InetAddress.getByName(server); } catch (UnknownHostException e) { // TODO Auto-generated catch block LOG.info("Fail to get address of ", server); continue; } if (ia.isLoopbackAddress() || ia.isAnyLocalAddress()) { ret.add(NetWorkUtils.ip()); } else { ret.add(ia.getHostAddress()); } } return JStormUtils.mk_list(ret); }
From source file:org.fdroid.enigtext.mms.MmsCommunication.java
private static void checkRouteToHost(Context context, String host, boolean usingMmsRadio) throws IOException { InetAddress inetAddress = InetAddress.getByName(host); if (!usingMmsRadio) { if (inetAddress.isSiteLocalAddress()) { throw new IOException("RFC1918 address in non-MMS radio situation!"); }/* www . j ava 2 s . co m*/ return; } Log.w("MmsCommunication", "Checking route to address: " + host + " , " + inetAddress.getHostAddress()); byte[] ipAddressBytes = inetAddress.getAddress(); if (ipAddressBytes != null && ipAddressBytes.length == 4) { int ipAddress = Conversions.byteArrayToIntLittleEndian(ipAddressBytes, 0); ConnectivityManager manager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); if (!manager.requestRouteToHost(MmsDownloader.TYPE_MOBILE_MMS, ipAddress)) throw new IOException("Connection manager could not obtain route to host."); } }
From source file:gridool.util.GridUtils.java
/** * @return hostname(255.255.255.255):77777 *//*from w w w . j a va 2 s .co m*/ public static String toHostNameAddrPort(@Nonnull final GridNode node) { InetAddress addr = node.getPhysicalAdress(); String hostname = addr.getHostName(); String ipAddr = addr.getHostAddress(); int port = node.getPort(); return hostname + '(' + ipAddr + "):" + port; }