Java Local Address Get getLocalAddress()

Here you can find the source of getLocalAddress()

Description

Gets the local address.

License

Apache License

Return

the local address

Declaration

public static String getLocalAddress() 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;

import java.util.Enumeration;

public class Main {
    /**/*from   ww w  .  j  a va  2 s .  com*/
     * Gets the local address.
     *
     * @return the local address
     */
    public static String getLocalAddress() {
        Enumeration<NetworkInterface> allNetInterfaces;
        try {
            allNetInterfaces = NetworkInterface.getNetworkInterfaces();
        } catch (SocketException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "NO";
        }
        InetAddress ip = null;
        String ips = "";
        while (allNetInterfaces.hasMoreElements()) {
            NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();

            Enumeration addresses = netInterface.getInetAddresses();
            while (addresses.hasMoreElements()) {
                ip = (InetAddress) addresses.nextElement();
                if (ip != null && ip instanceof Inet4Address) {
                    ips += ip.getHostAddress() + " ";
                }
            }
        }
        return ips;
    }
}

Related

  1. getLocalAddress()
  2. getLocalAddress()
  3. getLocalAddress()
  4. getLocalAddress()
  5. getLocalAddress()
  6. getLocalAddress()
  7. getLocalAddress()
  8. getLocalAddress()
  9. getLocalAddress()