Java Local Address Get getLocalAddress()

Here you can find the source of getLocalAddress()

Description

get Local Address

License

Open Source License

Declaration

public static InetAddress getLocalAddress() 

Method Source Code

//package com.java2s;

import java.net.InetAddress;
import java.net.NetworkInterface;

import java.util.Enumeration;

public class Main {
    public static InetAddress getLocalAddress() {
        InetAddress addr = null;//from   ww w  . j  a va  2s.  c o m
        try {
            Enumeration netInterfaces = NetworkInterface.getNetworkInterfaces();
            InetAddress ip = null;
            while (netInterfaces.hasMoreElements()) {
                NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();
                ip = (InetAddress) ni.getInetAddresses().nextElement();
                if (!ip.isLoopbackAddress()) {
                    addr = ip;
                    break;
                }
            }
            addr = addr != null ? addr : InetAddress.getLocalHost();
        } catch (Exception e) {
        }
        return addr;

    }
}

Related

  1. getLocalAddr()
  2. getLocalAddr()
  3. getLocalAddress()
  4. getLocalAddress()
  5. getLocalAddress()
  6. getLocalAddress()
  7. getLocalAddress()