Java Utililty Methods Local Address Get

List of utility methods to do Local Address Get

Description

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

Method

StringgetLocalAddressAsString()
get Local Address As String
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces != null && interfaces.hasMoreElements()) {
    Enumeration<InetAddress> addresses = interfaces.nextElement().getInetAddresses();
    while (addresses != null && addresses.hasMoreElements()) {
        InetAddress address = addresses.nextElement();
        if (acceptableAddress(address)) {
            return address.getHostAddress();
throw new UnknownHostException();
InetAddress[]getLocalAddresses()
Returns list of machine assigned IP addresses
List<InetAddress> addrs = new ArrayList<InetAddress>();
try {
    for (Enumeration<NetworkInterface> e1 = NetworkInterface.getNetworkInterfaces(); e1
            .hasMoreElements();) {
        NetworkInterface iface = e1.nextElement();
        for (Enumeration<InetAddress> e2 = iface.getInetAddresses(); e2.hasMoreElements();) {
            InetAddress inetAddr = e2.nextElement();
            if (inetAddr instanceof Inet4Address) {
...
SetgetLocalAddresses()
Get a set of local host addresses.
Set<InetAddress> result = new HashSet<InetAddress>();
for (Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements();) {
    NetworkInterface ni = e.nextElement();
    for (Enumeration<InetAddress> e2 = ni.getInetAddresses(); e2.hasMoreElements();) {
        InetAddress niAddress = e2.nextElement();
        result.add(niAddress);
return result;
StringgetLocalAddresses()
get Local Addresses
String host = InetAddress.getLocalHost().getCanonicalHostName();
String address = host;
String localAddress = "";
InetAddress inet = getRealIPAdrress();
if (inet != null) {
    localAddress = inet.toString();
} else {
    localAddress = InetAddress.getLocalHost().getHostAddress();
...
InetAddress[]getLocalAddresses()
get Local Addresses
Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
List<InetAddress> ret = new ArrayList<InetAddress>();
while (e.hasMoreElements()) {
    NetworkInterface i = e.nextElement();
    Enumeration<InetAddress> addresses = i.getInetAddresses();
    while (addresses.hasMoreElements()) {
        ret.add(addresses.nextElement());
return ret.toArray(new InetAddress[] {});
InetAddressgetLocalAddressFromNetworkInterfacesListeningOnPort(int pPort)
get Local Address From Network Interfaces Listening On Port
try {
    Enumeration<NetworkInterface> networkInterfaces;
    networkInterfaces = NetworkInterface.getNetworkInterfaces();
    while (networkInterfaces.hasMoreElements()) {
        NetworkInterface nif = networkInterfaces.nextElement();
        for (Enumeration<InetAddress> addrEnum = nif.getInetAddresses(); addrEnum.hasMoreElements();) {
            InetAddress interfaceAddress = addrEnum.nextElement();
            if (!interfaceAddress.isLoopbackAddress() && checkMethod(nif, isUp)
...
String[]getLocalAddressStrings()
Return all valid IP addresses for this local host.
String[] localAddressStrings = null;
if (localAddress != null) {
    InetAddress[] allMyAddresses = getLocalAddresses();
    if (allMyAddresses != null) {
        localAddressStrings = new String[allMyAddresses.length];
        for (int y = 0; y < allMyAddresses.length; y++) {
            localAddressStrings[y] = allMyAddresses[y].getHostAddress();
return localAddressStrings;
InetAddressgetLocalAddressWithMulticast()
Get a local address which supports multicast.
InetAddress addr = InetAddress.getLocalHost();
NetworkInterface nif = NetworkInterface.getByInetAddress(addr);
if (addr.isLoopbackAddress() || addr instanceof Inet6Address || !isMulticastSupported(nif)) {
    InetAddress lookedUpAddr = findLocalAddressViaNetworkInterface();
    if (lookedUpAddr != null) {
        return lookedUpAddr;
    addr = InetAddress.getByName("127.0.0.1");
...
InetAddress[]getLocalAddrs()
get Local Addrs
String host = InetAddress.getLocalHost().getCanonicalHostName();
return InetAddress.getAllByName(host);
InetAddressgetLocalInet4Address()
Returns the IPv4 address of the local host.
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();
    while (addresses.hasMoreElements()) {
        InetAddress current_addr = addresses.nextElement();
...