Java Utililty Methods InetAddress Create

List of utility methods to do InetAddress Create

Description

The list of methods to do InetAddress Create are organized into topic(s).

Method

VectorgetActiveInterfaceInetAddresses()
get Active Interface Inet Addresses
Vector<InetAddress> CurrentInterfaceIPs = new Vector<InetAddress>();
try {
    for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
            .hasMoreElements();) {
        NetworkInterface intf = en.nextElement();
        for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
            InetAddress inetAddress = enumIpAddr.nextElement();
            if (!inetAddress.isLoopbackAddress()) {
...
ListgetAddressInfoFindAll(Function convertValue, Predicate doBreak)
get Address Info Find All
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
List<String> findAll = new ArrayList<>();
END: while (networkInterfaces.hasMoreElements()) {
    NetworkInterface nextElement = networkInterfaces.nextElement();
    if (nextElement.isVirtual())
        continue;
    if (nextElement.isLoopback())
        continue;
...
intgetAddressPriority(InetAddress addr)
get Address Priority
if (addr.isLoopbackAddress() || addr.isAnyLocalAddress() || addr.isMulticastAddress())
    return 0;
byte[] octets = addr.getAddress();
if ((octets[0] == 10) || (octets[0] == (byte) 172 && octets[1] >= 16 && octets[1] <= 32)
        || (octets[0] == (byte) 192 && octets[1] == (byte) 168))
    return 1;
return 2;
StringgetAddrInfo(InetAddress pAddr)
get Addr Info
String ret = pAddr.getHostName() != null ? pAddr.getHostName() + " (" + pAddr.getHostAddress() + ")"
        : pAddr.getHostAddress();
ret += " [site-local: " + pAddr.isSiteLocalAddress() + ", link-local: " + pAddr.isLinkLocalAddress()
        + ", lb: " + pAddr.isLoopbackAddress() + "]";
NetworkInterface nif = NetworkInterface.getByInetAddress(pAddr);
ret += " -- nif: " + getNetworkInterfaceInfo(nif);
return ret;
ListgetAllInetAddress(final String interfaceName)
get All Inet Address
final List<InetAddress> ret = new ArrayList<>();
final Enumeration<NetworkInterface> netInterfaces = getInterfaces(interfaceName);
while (netInterfaces.hasMoreElements()) {
    final NetworkInterface networkInterface = netInterfaces.nextElement();
    for (final Enumeration<InetAddress> loopInetAddress = networkInterface
            .getInetAddresses(); loopInetAddress.hasMoreElements();) {
        final InetAddress tempInetAddress = loopInetAddress.nextElement();
        ret.add(tempInetAddress);
...
SetgetAllIPv4InetAddresses()
Get all the local IPV4 addresses without the loopback one
Set<Inet4Address> result = new HashSet<Inet4Address>();
Set<NetworkInterface> iset = getAllInterfaces();
for (NetworkInterface networkInterface : iset) {
    Enumeration<InetAddress> e = networkInterface.getInetAddresses();
    while (e.hasMoreElements()) {
        InetAddress elem = e.nextElement();
        if (elem instanceof Inet4Address) {
            result.add((Inet4Address) elem);
...
SetgetAllLocalIPv4InetAddresses()
Get all the IPv4 InetAddress of the local host which are not loopback ones
Set<Inet4Address> result = new HashSet<Inet4Address>();
Set<NetworkInterface> iset = getAllLocalInterfaces();
for (NetworkInterface networkInterface : iset) {
    Enumeration<InetAddress> e = networkInterface.getInetAddresses();
    while (e.hasMoreElements()) {
        InetAddress elem = e.nextElement();
        if (elem instanceof Inet4Address) {
            result.add((Inet4Address) elem);
...
InetAddressgetBroadcast(InetAddress address)
Gets the broadcast address for the network interface identified by the given address.
NetworkInterface inter = getInterface(address);
if (inter != null) {
    for (InterfaceAddress iaddr : inter.getInterfaceAddresses()) {
        if (iaddr.getAddress().equals(address)) {
            return iaddr.getBroadcast();
return null;
byte[]getBytes(InetAddress addr, int port)
Returns the IP:Port as byte array.
if (!isValidPort(port)) {
    throw new IllegalArgumentException("Port out of range: " + port);
byte[] address = addr.getAddress();
byte[] dst = new byte[address.length + 2];
System.arraycopy(address, 0, dst, 0, address.length);
dst[dst.length - 2] = (byte) ((port >> 8) & 0xFF);
dst[dst.length - 1] = (byte) ((port) & 0xFF);
...
intgetClassPart(InetAddress ip, int partNumber)
Returns the n-th part of an InetAddress.
String[] parts = ip.getHostAddress().split("\\.");
String part = parts[partNumber];
return new Integer(part).intValue();