Java IP Address Get getLocalIpv4()

Here you can find the source of getLocalIpv4()

Description

get Local Ipv

License

Apache License

Declaration

public static List<String> getLocalIpv4() 

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.ArrayList;
import java.util.Enumeration;
import java.util.List;

public class Main {
    public static List<String> getLocalIpv4() {
        try {/*from  w ww  . ja  v  a2s  . c  om*/
            List<String> ips = new ArrayList<String>();
            for (Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); interfaces
                    .hasMoreElements();) {
                NetworkInterface networkInterface = interfaces.nextElement();
                if (networkInterface.isLoopback() || networkInterface.isVirtual() || !networkInterface.isUp()) {
                    continue;
                }
                Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
                if (addresses.hasMoreElements()) {
                    InetAddress nextElement = addresses.nextElement();
                    if (nextElement instanceof Inet4Address) {
                        ips.add(nextElement.getHostAddress());
                    }
                }
            }
            return ips;
        } catch (SocketException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }
}

Related

  1. getLocalIPList()
  2. getLocalIPs()
  3. getLocalIPs()
  4. getLocalIps()
  5. getLocalIPs(boolean refresh)
  6. getLocalIPv6Address()
  7. getLocalNetWorkIp()
  8. getLocalV4Ip()
  9. getLocalV4IpList()