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

InetAddressgetLocalIpAddress()
get Local Ip Address
try {
    InetAddress localAddress = null;
    for (Enumeration<?> networkInterfaces = NetworkInterface.getNetworkInterfaces(); networkInterfaces
            .hasMoreElements();) {
        NetworkInterface networkInterface = (NetworkInterface) networkInterfaces.nextElement();
        for (Enumeration<?> ipAddresses = networkInterface.getInetAddresses(); ipAddresses
                .hasMoreElements();) {
            InetAddress ipAddress = (InetAddress) ipAddresses.nextElement();
...
StringgetLocalIpAddress()
get Local Ip Address
String ipAddress = null;
Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
while (en.hasMoreElements()) {
    NetworkInterface e = en.nextElement();
    Enumeration<InetAddress> addresses = e.getInetAddresses();
    while (addresses.hasMoreElements()) {
        InetAddress address = addresses.nextElement();
        if (!address.isLoopbackAddress() && address.isSiteLocalAddress()) {
...
ListgetLocalIPAddresses()
Gets local IPv4 addresses of the local host.
List<InetAddress> result = new ArrayList<>();
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
    NetworkInterface ni = interfaces.nextElement();
    if ((!ni.isUp() || ni.isLoopback()) || ni.isVirtual()) {
        continue;
    Enumeration<InetAddress> ips = ni.getInetAddresses();
...
ListgetLocalIpAddresses()
Returns a list of local InetAddresses for this machine
return getLocalIpAddresses(false, false);
ListgetLocalIPAddresses()
get Local IP Addresses
final ArrayList<String> iplist = new ArrayList<String>(1);
final Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
    final NetworkInterface ni = interfaces.nextElement();
    final Enumeration<InetAddress> ipaddresses = ni.getInetAddresses();
    while (ipaddresses.hasMoreElements()) {
        final String ip = ipaddresses.nextElement().getHostAddress();
        if (ip.contains(".") && !ip.contains("127.0.0.")) {
...
StringgetLocalIpByInterfaceName(String interfaceName)
get Local Ip By Interface Name
Enumeration<InetAddress> addressEnumeration = NetworkInterface.getByName(interfaceName).getInetAddresses();
while (addressEnumeration.hasMoreElements()) {
    InetAddress address = addressEnumeration.nextElement();
    if (address instanceof Inet4Address) {
        return address.getHostAddress();
return null;
...
StringgetLocalIPByNetworkInterface()
get Local IP By Network Interface
Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces();
while (enumeration.hasMoreElements()) {
    NetworkInterface networkInterface = enumeration.nextElement();
    if (networkInterface.isUp() && !networkInterface.isVirtual() && !networkInterface.isLoopback()) {
        Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
        while (addresses.hasMoreElements()) {
            InetAddress inetAddress = addresses.nextElement();
            if (inetAddress instanceof Inet4Address) {
...
CollectiongetLocalIPCollection()
based on http://pastebin.com/5X073pUc

Returns all available IP addresses.

List<InetAddress> addressList = Lists.newArrayList();
Enumeration<NetworkInterface> nifs = NetworkInterface.getNetworkInterfaces();
if (nifs == null) {
    return addressList;
while (nifs.hasMoreElements()) {
    NetworkInterface nif = nifs.nextElement();
    Enumeration<InetAddress> adrs = nif.getInetAddresses();
...
ListgetLocalIPList()
get Local IP List
List<InetAddress> result = new ArrayList<InetAddress>();
Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
for (NetworkInterface net : Collections.list(nets)) {
    if (!net.isLoopback() && !net.isVirtual() && !net.isPointToPoint() && net.isUp()) {
        for (InetAddress curAddress : Collections.list(net.getInetAddresses())) {
            if (curAddress != null && !curAddress.isMulticastAddress()
                    && curAddress instanceof Inet4Address) {
                result.add(curAddress);
...
ListgetLocalIps()
get Local Ips
List<String> ipList = new ArrayList<String>(2);
NetworkInterface ni = null;
Enumeration<InetAddress> addresses = null;
InetAddress address = null;
Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces();
while (enumeration.hasMoreElements()) {
    ni = enumeration.nextElement();
    if (ni.isLoopback() || ni.isVirtual() || !ni.isUp()) {
...