Java InetAddress Create getInetAddress()

Here you can find the source of getInetAddress()

Description

get Inet Address

License

Open Source License

Declaration

private static InetAddress getInetAddress() 

Method Source Code


//package com.java2s;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Collections;
import java.util.Enumeration;

public class Main {
    private static InetAddress getInetAddress() {
        InetAddress currentAddress = null;
        try {//from   w  w  w.  ja  va 2s. c o  m
            InetAddress addrs[] = InetAddress.getAllByName(InetAddress.getLocalHost().getHostName());

            for (InetAddress addr : addrs) {
                if (!addr.isLoopbackAddress() && addr.isSiteLocalAddress()) {
                    currentAddress = addr;
                    break;
                }
            }
            if (currentAddress == null || currentAddress.getHostAddress() == null
                    || currentAddress.getHostAddress().length() == 0) {
                try {
                    Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();
                    for (NetworkInterface iface : Collections.list(ifaces)) {
                        Enumeration<InetAddress> raddrs = iface.getInetAddresses();
                        for (InetAddress raddr : Collections.list(raddrs)) {
                            if (!raddr.isLoopbackAddress() && raddr.isSiteLocalAddress()) {
                                currentAddress = raddr;
                                break;
                            }
                        }
                    }
                } catch (SocketException e) {
                    e.printStackTrace();
                }
            }
        } catch (UnknownHostException e) {
        }
        if (currentAddress != null && currentAddress.getHostAddress() != null
                && currentAddress.getHostAddress().length() > 0) {
            return currentAddress;
        }
        return null;
    }
}

Related

  1. getHostNameReliably(final String requestingHost, final InetAddress site, final URL requestingUrl)
  2. getHostNameWithoutDomain(final InetAddress addr)
  3. getInetAddress()
  4. getInetAddress()
  5. getInetAddress()
  6. getInetAddress(final int[] octets, final int offset, final int length)
  7. getInetAddress(final String addressOrName)
  8. getInetAddress(long ip)
  9. getInetAddress(Socket socket)