Java Local Address Get getLocalAddress()

Here you can find the source of getLocalAddress()

Description

from: http://www.java2s.com/Code/Java/Network-Protocol/FindsalocalnonloopbackIPv4address.htm Finds a local, non-loopback, IPv4 address

License

Open Source License

Exception

Parameter Description
SocketException If there was a problem querying the network interfaces

Return

The first non-loopback IPv4 address found, or null if no such addresses found

Declaration

public static InetAddress getLocalAddress() 

Method Source Code


//package com.java2s;
/*/*from  w  w w .j  a  v a2  s  .c o m*/
 * Battlesheep is a funny remake of the famous Battleship game, developed
 * as a distributed system.
 * 
 * Copyright (C) 2016 - Giulio Biagini, Michele Corazza, Gianluca Iselli
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

public class Main {
    /**
     * from: http://www.java2s.com/Code/Java/Network-Protocol/FindsalocalnonloopbackIPv4address.htm
     * 
     * Finds a local, non-loopback, IPv4 address
     * 
     * @return The first non-loopback IPv4 address found, or <code>null</code>
     *         if no such addresses found
     * @throws SocketException
     *             If there was a problem querying the network interfaces
     */
    public static InetAddress getLocalAddress() {
        try {
            Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();
            while (ifaces.hasMoreElements()) {
                NetworkInterface iface = ifaces.nextElement();
                Enumeration<InetAddress> addresses = iface.getInetAddresses();

                while (addresses.hasMoreElements()) {
                    InetAddress addr = addresses.nextElement();
                    //System.out.println("addr: " + addr.getHostAddress());
                    if (addr instanceof Inet4Address && !addr.isLoopbackAddress()) {
                        return addr;
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return null;
    }
}

Related

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