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

StringgetLanIPAddress()
get Lan IP Address
for (final Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); interfaces
        .hasMoreElements();) {
    final NetworkInterface cur = interfaces.nextElement();
    if (cur.isLoopback()) {
        continue;
    for (final InterfaceAddress addr : cur.getInterfaceAddresses()) {
        final InetAddress inet_addr = addr.getAddress();
...
StringgetLinuxIPAddress()
get Linux IP Address
String hostAddr = "";
try {
    Enumeration<NetworkInterface> nienum = NetworkInterface.getNetworkInterfaces();
    while (nienum.hasMoreElements()) {
        NetworkInterface ni = nienum.nextElement();
        Enumeration<InetAddress> kk = ni.getInetAddresses();
        while (kk.hasMoreElements()) {
            InetAddress inetAddress = kk.nextElement();
...
ListgetLocalAddresses(boolean ipv4only)
get Local Addresses
List<InetAddress> result = new ArrayList<>();
for (Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements();) {
    NetworkInterface nif = e.nextElement();
    for (Enumeration<InetAddress> en = nif.getInetAddresses(); en.hasMoreElements();) {
        InetAddress addr = en.nextElement();
        if (ipv4only && !(addr instanceof Inet4Address))
            continue;
        result.add(addr);
...
StringgetLocalHostIp()
get Local Host Ip
String ip = "";
InetAddress inetAddress;
try {
    inetAddress = InetAddress.getByName("localhost");
    ip = inetAddress.getHostAddress();
} catch (UnknownHostException e) {
    throw new RuntimeException("Is your network broken; localhost does not exist?", e);
return ip;
StringgetLocalHostIp()
get Local Host Ip
String regex = "[\\d]+.[\\d]+.[\\d]+.[\\d]+";
Enumeration<?> e = null;
try {
    e = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e1) {
    e1.printStackTrace();
while (e.hasMoreElements()) {
...
StringgetLocalHostIP()
Returns the IP address of the local host as string.
String result;
if (m_IP == null) {
    try {
        result = InetAddress.getLocalHost().getHostAddress();
    } catch (Exception e) {
        result = getIPFromNetworkInterface();
    m_IP = result;
...
StringgetLocalhostIp()
get Localhost Ip
try {
    for (final Enumeration<NetworkInterface> interfaces = NetworkInterface
            .getNetworkInterfaces(); interfaces.hasMoreElements();) {
        final NetworkInterface cur = interfaces.nextElement();
        if (cur.isLoopback()) {
            continue;
        if (cur.getName().startsWith("vnic")) {
...
StringgetLocalHostIPAdress()
Get Local IP Address
String ipAddr = "";
try {
    InetAddress inetAddr = InetAddress.getLocalHost();
    byte[] addr = inetAddr.getAddress();
    for (int i = 0; i < addr.length; i++) {
        if (i > 0) {
            ipAddr += ".";
        ipAddr += addr[i] & 0xFF;
} catch (Exception e) {
    e.printStackTrace();
return ipAddr;
StringgetLocalHostIps()
get Local Host Ips
if (localHostIps.equals("")) {
    StringBuffer sb = new StringBuffer();
    InetAddress[] ias;
    try {
        ias = InetAddress.getAllByName(InetAddress.getLocalHost().getHostName());
        for (int i = 0; i < ias.length; i++) {
            String ip = ias[i].getHostAddress();
            sb.append(ip);
...
StringgetLocalHostIps()
get Local Host Ips
StringBuffer sb = new StringBuffer();
final char flag = 2;
try {
    Enumeration<?> netInterfaces = NetworkInterface.getNetworkInterfaces();
    while (netInterfaces.hasMoreElements()) {
        NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();
        Enumeration<InetAddress> ips = ni.getInetAddresses();
        while (ips.hasMoreElements()) {
...