Here you can find the source of getLocalAddresses(boolean ipv4only)
private static List<InetAddress> getLocalAddresses(boolean ipv4only) throws SocketException
//package com.java2s; 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 { private static List<InetAddress> getLocalAddresses(boolean ipv4only) throws SocketException { List<InetAddress> result = new ArrayList<>(); for (Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements();) { NetworkInterface nif = e.nextElement(); for (Enumeration<InetAddress> en = nif.getInetAddresses(); en.hasMoreElements();) { InetAddress addr = en.nextElement(); if (ipv4only && !(addr instanceof Inet4Address)) continue; result.add(addr);/* w w w. j a v a 2 s.co m*/ } } return result; } }