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

StringgetIpAddress()
get Ip Address
String ip = null;
try {
    Enumeration<NetworkInterface> er = NetworkInterface.getNetworkInterfaces();
    while (er.hasMoreElements()) {
        NetworkInterface ni = er.nextElement();
        if (ni.getName().startsWith("eth") || ni.getName().startsWith("bond")) {
            List<InterfaceAddress> list = ni.getInterfaceAddresses();
            for (InterfaceAddress interfaceAddress : list) {
...
StringgetIpAddress()
Return a non loopback IPv4 address for the machine running this process.
try {
    for (Enumeration<NetworkInterface> enumNic = NetworkInterface.getNetworkInterfaces(); enumNic
            .hasMoreElements();) {
        NetworkInterface ifc = enumNic.nextElement();
        if (ifc.isUp()) {
            for (Enumeration<InetAddress> enumAddr = ifc.getInetAddresses(); enumAddr.hasMoreElements();) {
                InetAddress address = enumAddr.nextElement();
                if (address instanceof Inet4Address && !address.isLoopbackAddress()) {
...
StringgetIPAddress(boolean useIPv4)
Get IP address from first non-localhost interface
try {
    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 (addr instanceof Inet4Address) {
...
StringgetIPAddress(final String hostName)
Retrieves a host's IP address; see SOAPClient.getLocalAddr() for local IP
try {
    return InetAddress.getByName(hostName).getHostAddress();
} catch (final Exception e) {
    throw toRuntimeException(e);
StringgetIPAddress(NetworkInterface e)
get IP Address
Enumeration<InetAddress> a = e.getInetAddresses();
for (; a.hasMoreElements();) {
    InetAddress addr = a.nextElement(); 
    addr = a.nextElement(); 
    return addr.getHostAddress();
return null;
StringgetIPAddress(String hostname)

Determines a host's IP address in string form.

String ipAddr = null;
try {
    InetAddress ia = InetAddress.getByName(hostname);
    ipAddr = ia.getHostAddress();
} catch (UnknownHostException e) {
return ipAddr;
StringgetIpAddress(String server)
Perform a lookup of server returning the associated ip address.
if (server == null)
    return null;
try {
    InetAddress addr = InetAddress.getByName(server);
    return addr.getHostAddress();
} catch (UnknownHostException uhe) {
    return null;
ListgetIPAddresses()
get IP Addresses
List<String> ips = new ArrayList<String>();
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
    NetworkInterface current = interfaces.nextElement();
    if (!current.isUp() || current.isLoopback() || current.isVirtual()) {
        continue;
    Enumeration<InetAddress> addresses = current.getInetAddresses();
...
ListgetIpAddresses()
get Ip Addresses
try {
    List<InetAddress> addrList = new ArrayList<InetAddress>();
    Enumeration<NetworkInterface> enumNI = NetworkInterface.getNetworkInterfaces();
    while (enumNI.hasMoreElements()) {
        NetworkInterface ifc = enumNI.nextElement();
        if (ifc.isUp()) {
            Enumeration<InetAddress> enumAdds = ifc.getInetAddresses();
            while (enumAdds.hasMoreElements()) {
...
String[]getIpAddresses()
get Ip Addresses
List addresses = new ArrayList();
Enumeration e = NetworkInterface.getNetworkInterfaces();
while (e.hasMoreElements()) {
    NetworkInterface netface = (NetworkInterface) e.nextElement();
    Enumeration e2 = netface.getInetAddresses();
    while (e2.hasMoreElements()) {
        InetAddress ip = (InetAddress) e2.nextElement();
        if (isValidIpAddress(ip)) {
...