Java IP Address Get getLocalIP()

Here you can find the source of getLocalIP()

Description

get Local IP

License

Open Source License

Declaration

public static String getLocalIP() throws SocketException 

Method Source Code

//package com.java2s;
/**// ww w  .ja  v  a  2s  .  co m
* License: https://github.com/votingsystem/votingsystem/wiki/Licencia
*/

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

import java.util.*;

public class Main {
    public static String getLocalIP() throws SocketException {
        Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
        for (NetworkInterface netint : Collections.list(nets)) {
            Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
            for (InetAddress inetAddress : Collections.list(inetAddresses)) {
                if (inetAddress.isSiteLocalAddress()) {
                    String inetAddressStr = inetAddress.toString();
                    while (inetAddressStr.startsWith("/"))
                        inetAddressStr = inetAddressStr.substring(1);
                    return inetAddressStr;
                }

            }
        }
        return null;
    }
}

Related

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