Java Utililty Methods IP Address Get

List of utility methods to do IP Address Get

Description

The list of methods to do IP Address Get are organized into topic(s).

Method

StringgetExternalIPAddress()
get External IP Address
BufferedReader in = null;
try {
    URL whatismyip = new URL("http://automation.whatismyip.com/n09230945.asp");
    in = new BufferedReader(new InputStreamReader(whatismyip.openStream()));
    return in.readLine(); 
} catch (IOException ex) {
} finally {
    try {
...
StringgetFirstLocalNonLoopbackIpAddress()
get First Local Non Loopback Ip Address
SortedSet<String> addresses = new TreeSet<String>();
Iterator<NetworkInterface> iterator = localInterfaces.iterator();
while (iterator.hasNext()) {
    NetworkInterface networkInterface = iterator.next();
    Enumeration<InetAddress> inetAddressEnumeration = networkInterface.getInetAddresses();
    while (inetAddressEnumeration.hasMoreElements()) {
        InetAddress address = inetAddressEnumeration.nextElement();
        if (!address.isLoopbackAddress() && !address.getHostAddress().contains(":")) {
...
InetAddressgetFirstNonLoopbackAddress(boolean preferIpv4, boolean preferIPv6)
Get the first non-loopback address.
Enumeration en = NetworkInterface.getNetworkInterfaces();
while (en.hasMoreElements()) {
    NetworkInterface i = (NetworkInterface) en.nextElement();
    for (Enumeration en2 = i.getInetAddresses(); en2.hasMoreElements();) {
        InetAddress addr = (InetAddress) en2.nextElement();
        if (!addr.isLoopbackAddress()) {
            if (addr instanceof Inet4Address) {
                if (preferIPv6) {
...
StringgetFirstNonLoopBackAddress(boolean preferIpv4, boolean preferIPv6)
get First Non Loop Back Address
final Enumeration en = NetworkInterface.getNetworkInterfaces();
while (en.hasMoreElements()) {
    final NetworkInterface i = (NetworkInterface) en.nextElement();
    for (Enumeration en2 = i.getInetAddresses(); en2.hasMoreElements();) {
        final InetAddress address = (InetAddress) en2.nextElement();
        if (!address.isLoopbackAddress()) {
            if (address instanceof Inet4Address) {
                if (preferIPv6) {
...
StringgetHostIp()
get Host Ip
if (null == getInetAddress()) {
    return null;
String ip = getInetAddress().getHostAddress(); 
return ip;
HashSetgetHostIP()
get Host IP
HashSet ipAdd = new HashSet();
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() && !inetAddress.isLinkLocalAddress()
...
StringgetHostIp()
get Host Ip
if (_hostIp == null) {
    try {
        if (System.getProperty("host.ip.forced") != null
                && !System.getProperty("host.ip.forced").equals("")) {
            _hostIp = System.getProperty("host.ip.forced");
        } else {
                if (publicIPUnavilable()) {
...
StringgetHostIP()
Find the IP address of localhost.
try {
    byte[] ipAddr = InetAddress.getLocalHost().getAddress();
    StringBuilder b = new StringBuilder();
    if (ipAddr != null && ipAddr.length > 0) {
        b.append(ipAddr[0]);
        for (int i = 1; i < ipAddr.length; i++) {
            b.append('.').append(ipAddr[i]);
        return b.toString();
} catch (UnknownHostException e) {
return null;
StringgetHostIP()
Method to return the IP address of the host computer
try {
    InetAddress thisIp = InetAddress.getLocalHost();
    return thisIp.getHostAddress();
} catch (Exception e) {
    e.printStackTrace();
    return "No IP Found";
StringgetHostIpAddress()
Returns the IP address hosting this JVM, or null if it could not be determined.
try {
    InetAddress host = InetAddress.getLocalHost();
    return host.getHostAddress();
} catch (Exception ex) {
return null;