Java Local Address Get getLocalInet4Address()

Here you can find the source of getLocalInet4Address()

Description

get Local Inet Address

License

Open Source License

Return

a list of IPv4 addresses for the all local network cards

Declaration

public static List<InetAddress> getLocalInet4Address() 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    /**//from w w w.  ja va  2  s.  c  o  m
     * @return a list of IPv4 addresses for the all local network cards
     */
    public static List<InetAddress> getLocalInet4Address() {
        ArrayList<InetAddress> ips = new ArrayList<>();
        try {
            Enumeration<NetworkInterface> netIntf = NetworkInterface.getNetworkInterfaces();
            while (netIntf.hasMoreElements()) {
                Enumeration<InetAddress> addresses = netIntf.nextElement().getInetAddresses();
                while (addresses.hasMoreElements()) {
                    InetAddress ip = addresses.nextElement();
                    if (ip instanceof Inet4Address && ip.isSiteLocalAddress())
                        ips.add(ip);
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return ips;
    }
}

Related

  1. getLocalAddressFromNetworkInterfacesListeningOnPort(int pPort)
  2. getLocalAddressStrings()
  3. getLocalAddressWithMulticast()
  4. getLocalAddrs()
  5. getLocalInet4Address()
  6. getLocalInternetProtocolAddress()
  7. getLocalMachineAddress()
  8. getLocalNetAddress()