Java Utililty Methods IP Address Get

List of utility methods to do IP Address Get

Description

The list of methods to do IP Address Get are organized into topic(s).

Method

StringgetIPs()
get I Ps
try {
    Enumeration<NetworkInterface> nets;
    NetworkInterface n;
    Enumeration<InetAddress> addrs;
    InetAddress a;
    String s = "";
    nets = NetworkInterface.getNetworkInterfaces();
    while (nets.hasMoreElements()) {
...
StringgetIPv4Address()
get I Pv Address
try {
    Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
    NetworkInterface itf = null;
    while (en.hasMoreElements()) {
        itf = en.nextElement();
        if (!itf.isLoopback()) {
            Enumeration<InetAddress> addresses = itf.getInetAddresses();
            InetAddress addr = null;
...
StringgetIPV4Address(List addresses)
Finds an ipv4 address in a list of interface addresses
for (InterfaceAddress interfaceAddress : addresses) {
    if (interfaceAddress.getAddress() instanceof Inet4Address) {
        return interfaceAddress.getAddress().getHostAddress();
return null;
Inet4AddressgetIPv4Address(NetworkInterface iface)
get I Pv Address
for (InterfaceAddress interfaceAddress : iface.getInterfaceAddresses()) {
    if (interfaceAddress.getAddress() instanceof Inet4Address) {
        return (Inet4Address) interfaceAddress.getAddress();
return null;
Inet4AddressgetIPV4MainAddress(NetworkInterface ni)
Return the main ip V4 address associated with the ni
Inet4Address[] addresses = getIPV4AllAddresses(ni);
if (addresses != null && addresses.length > 0) {
    return addresses[addresses.length - 1];
return null;
InetAddressgetIPv4MulticastGroup(int hash)
get I Pv Multicast Group
return getIPv4MulticastGroup(hash, IPV4_MULTICAST_ALLOCATION_RANGE[0], IPV4_MULTICAST_ALLOCATION_RANGE[1]);
byte[]getIPV4NetwprkOrder(String theIp)
turn ip in string representation to byte array in network order.
byte[] toReturn = new byte[IPV4_ADDRESS_LEN];
String[] fields = theIp.split("\\.");
if (fields == null || fields.length < IPV4_ADDRESS_LEN) {
    throw new UnknownHostException();
for (int i = 0; i < fields.length; i++) {
    toReturn[i] = (byte) Integer.parseInt(fields[i]);
return toReturn;
int[]getIPV6AddrArray(String s)
get IPV Addr Array
try {
    InetAddress inetaddress = InetAddress.getByName(s);
    s = inetaddress.getHostAddress();
} catch (UnknownHostException unknownhostexception) {
    System.err.println("unknown ipv6 exception");
StringTokenizer stringtokenizer = new StringTokenizer(s, ":");
if (stringtokenizer.countTokens() != 8)
...
Inet6Address[]getIPV6AllAddresses(NetworkInterface ni)
Return all the ip V6 addresses associated with the ni
ArrayList<Inet6Address> v = new ArrayList<Inet6Address>();
for (Enumeration<InetAddress> e = ni.getInetAddresses(); e.hasMoreElements();) {
    InetAddress addr = e.nextElement();
    if (addr instanceof Inet6Address)
        v.add((Inet6Address) addr);
return v.toArray(new Inet6Address[v.size()]);
ListgetIpv6List()
Get all IPv6 addresses defined in local network adapters.
List<Inet6Address> addresses = new ArrayList<>();
try {
    for (Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces(); nis
            .hasMoreElements();) {
        NetworkInterface ni = nis.nextElement();
        for (Enumeration<InetAddress> ias2 = ni.getInetAddresses(); ias2.hasMoreElements();) {
            InetAddress ia = ias2.nextElement();
            if (ia instanceof Inet6Address) {
...