Java IP Address Get getLocalIPAddress()

Here you can find the source of getLocalIPAddress()

Description

Retrieve the IP address of the generation machine .

License

Open Source License

Return

the local machine IP address

Declaration

public static String getLocalIPAddress() 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2015 LegSem.//from w w  w .j  av  a  2s . co  m
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     LegSem - initial API and implementation
 ******************************************************************************/

import java.net.InetAddress;

import java.net.UnknownHostException;

public class Main {
    /**
     * Retrieve the IP address of the generation machine .
     * 
     * @return the local machine IP address
     */
    public static String getLocalIPAddress() {
        try {
            InetAddress addr = InetAddress.getLocalHost();
            byte[] ipAddr = addr.getAddress();
            String ipAddrStr = "";
            for (int i = 0; i < ipAddr.length; i++) {
                if (i > 0) {
                    ipAddrStr += ".";
                }
                ipAddrStr += ipAddr[i] & 0xFF;
            }
            return ipAddrStr;
        } catch (UnknownHostException e) {
            return "";
        }

    }
}

Related

  1. getLocalIP()
  2. getLocalIP()
  3. getLocalIP()
  4. getLocalIp()
  5. getLocalIp(final boolean useIpv6)
  6. getLocalIPAddress()
  7. getLocalIPAddress()
  8. getLocalIpAddress()
  9. getLocalIpAddress()