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

StringgetIp()
Returns the public IP if one isn't specified in the machine.ip System property
if (System.getProperty("machine.ip") != null) {
    return System.getProperty("machine.ip");
try (BufferedReader br = new BufferedReader(
        new InputStreamReader(new URL(IP_PROVIDER).openConnection().getInputStream()))) {
    String ip;
    if ((ip = br.readLine()) != null) {
        return ip;
...
StringgetIP()
get IP
String ipAddress = null;
Enumeration<NetworkInterface> net = null;
try {
    net = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e) {
    throw new RuntimeException(e);
while (net.hasMoreElements()) {
...
StringgetIp()
get Ip
String localip = null;
String netip = null;
try {
    Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
    InetAddress ip = null;
    boolean finded = false;
    while (netInterfaces.hasMoreElements() && !finded) {
        NetworkInterface ni = netInterfaces.nextElement();
...
byte[]getIpAddress()
get Ip Address
try {
    return getLocalHost().getAddress();
} catch (Exception e) {
    return new byte[] { 0, 0, 0, 0 };
StringgetIPAddress()
This function is used to get IP Address of the host machine.
try {
    final NetworkInterface niEth0 = NetworkInterface.getByName("eth0");
    final NetworkInterface niWlan0 = NetworkInterface.getByName("wlan0");
    if (niEth0 != null) {
        for (Enumeration en = niEth0.getInetAddresses(); en.hasMoreElements();) {
            final InetAddress addr = (InetAddress) en.nextElement();
            if (addr instanceof Inet4Address) {
                return addr.getHostAddress();
...
StringgetIpAddress()
get Ip Address
String rtip = "unknown.host";
try {
    Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
    InetAddress ip = null;
    while (allNetInterfaces.hasMoreElements()) {
        NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
        if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()) {
            continue;
...
StringgetIpAddress()
get Ip Address
String ipAddress = "";
try {
    Socket socket = new Socket("google.com", 80);
    InetAddress inetAddress = socket.getLocalAddress();
    if (inetAddress != null) {
        ipAddress = inetAddress.getHostAddress();
} catch (Exception e) {
...
StringgetIpAddress()
get Ip Address
InetAddress address = InetAddress.getLocalHost();
return address.getHostAddress();
StringgetIpAddress()
get Ip Address
String hostaddress = "localhost";
try {
    Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
    while (e.hasMoreElements()) {
        NetworkInterface n = e.nextElement();
        Enumeration<InetAddress> ee = n.getInetAddresses();
        while (ee.hasMoreElements()) {
            InetAddress i = ee.nextElement();
...
StringgetIpAddress()
get Ip Address
try {
    return InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
    return null;