Java Local Address Get getLocalAddresses()

Here you can find the source of getLocalAddresses()

Description

get Local Addresses

License

Open Source License

Declaration


static public String getLocalAddresses() throws UnknownHostException 

Method Source Code

//package com.java2s;
/*//from  ww w .ja  va 2 s .com
 * ISABEL: A group collaboration tool for the Internet
 * Copyright (C) 2009 Agora System S.A.
 * 
 * This file is part of Isabel.
 * 
 * Isabel is free software: you can redistribute it and/or modify
 * it under the terms of the Affero GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Isabel 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
 * Affero GNU General Public License for more details.
 * 
 * You should have received a copy of the Affero GNU General Public License
 * along with Isabel.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.net.*;
import java.util.*;

public class Main {

    static public String getLocalAddresses() throws UnknownHostException {

        //String host = InetAddress.getLocalHost().getHostName();

        String host = InetAddress.getLocalHost().getCanonicalHostName();

        //String host = InetAddress.getLocalHost().getHostAddress();

        //System.out.println("host: " + host);

        String address = host;

        String localAddress = "";

        //localAddress = InetAddress.getLocalHost().toString().split("/")[1];

        InetAddress inet = getRealIPAdrress();
        if (inet != null) {
            localAddress = inet.toString();
        } else {
            localAddress = InetAddress.getLocalHost().getHostAddress();
        }

        address = address + "/" + localAddress;

        return address;

    }

    public static InetAddress getRealIPAdrress() {
        Enumeration interfaces = null;
        try {
            interfaces = NetworkInterface.getNetworkInterfaces();
        } catch (SocketException e) {
            e.printStackTrace();
        }
        if (interfaces == null)
            return null;

        while (interfaces.hasMoreElements()) {
            NetworkInterface card = (NetworkInterface) interfaces
                    .nextElement();
            Enumeration addresses = card.getInetAddresses();
            if (addresses == null)
                continue;

            while (addresses.hasMoreElements()) {
                InetAddress address = (InetAddress) addresses.nextElement();

                String temp = address.getHostAddress();
                if (!address.isLoopbackAddress() && !temp.startsWith("10.")
                        && !temp.contains(":")) {
                    //si no empieza por "127.0" o "10." devuelvo la ip, o si no tiene ":" que indica que es ipv6
                    return address;
                }
            }
        }
        return null;
    }
}

Related

  1. getLocalAddress(String adaptorName)
  2. getLocalAddress(String filter)
  3. getLocalAddress0()
  4. getLocalAddressAsString()
  5. getLocalAddresses()
  6. getLocalAddresses()
  7. getLocalAddresses()
  8. getLocalAddressFromNetworkInterfacesListeningOnPort(int pPort)
  9. getLocalAddressStrings()