Java Local Address Get getLocalInternetProtocolAddress()

Here you can find the source of getLocalInternetProtocolAddress()

Description

get Local Internet Protocol Address

License

Apache License

Exception

Parameter Description
SocketException an exception

Return

The local Internet protocol address of this machine

Declaration

public static String getLocalInternetProtocolAddress() throws SocketException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

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

public class Main {
    /**//  w w  w . j a v a  2  s.c  o  m
     * @return The local Internet protocol address of this machine
     * @throws SocketException
     */
    public static String getLocalInternetProtocolAddress() throws SocketException {
        Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();

        while (networkInterfaces.hasMoreElements()) {
            for (InterfaceAddress interfaceAddress : networkInterfaces.nextElement().getInterfaceAddresses()) {
                if (interfaceAddress.getAddress().isSiteLocalAddress()) {
                    return interfaceAddress.getAddress().toString().replace("/", "");
                }
            }
        }

        throw new IllegalStateException("Expected the computer's local IP address but didn't get one!");
    }
}

Related

  1. getLocalAddressStrings()
  2. getLocalAddressWithMulticast()
  3. getLocalAddrs()
  4. getLocalInet4Address()
  5. getLocalInet4Address()
  6. getLocalMachineAddress()
  7. getLocalNetAddress()