Class NetworkUtility


  • public class NetworkUtility
    extends Object
    Utility class that can be used by any calling code to do common routines about Network I/O
    Author:
    Bongjae Chang
    • Constructor Detail

      • NetworkUtility

        public NetworkUtility()
    • Method Detail

      • getAllLocalAddresses

        public static List<InetAddress> getAllLocalAddresses()
        Returns all local addresses except for lookback and any local address But, if any addresses were not found locally, the lookback is added to the list.
        Returns:
        List which contains available addresses locally
      • getAnyAddress

        public static InetAddress getAnyAddress()
      • getLoopbackAddress

        public static InetAddress getLoopbackAddress()
      • getFirstNetworkInterface

        public static NetworkInterface getFirstNetworkInterface​(boolean preferIPv6)
                                                         throws IOException
        Return a first network interface except for the lookback But, if any network interfaces were not found locally, the lookback interface is returned.
        Parameters:
        preferIPv6 - flag to indicate if IPV6 is preferred
        Returns:
        a first network interface
        Throws:
        IOException - if an I/O error occurs or a network interface was not found
      • getLocalHostAddress

        public static InetAddress getLocalHostAddress()
      • getPreferIpv6Addresses

        public static boolean getPreferIpv6Addresses()
      • getFirstInetAddress

        public static InetAddress getFirstInetAddress()
                                               throws IOException
        Return a first InetAddress of the first network interface check java property java.net.preferIPv6Addresses for whether to favor IPv4 or IPv6. (java default is to favor IPv4 addresses) If unable to find a valid network interface, then fallback to trying to get localhost address as last resort.
        Returns:
        a first found InetAddress.
        Throws:
        IOException - if an I/O error occurs or a network interface was not found
      • getFirstInetAddress

        public static InetAddress getFirstInetAddress​(boolean preferIPv6)
                                               throws IOException
        Return a first InetAddress of the first network interface But, if any network interfaces were not found locally, null could be returned.
        Parameters:
        preferIPv6 - if true, prefer IPv6 InetAddress. otherwise prefer IPv4 InetAddress
        Returns:
        a first found InetAddress.
        Throws:
        IOException - if an I/O error occurs or a network interface was not found
      • getNetworkInetAddress

        public static InetAddress getNetworkInetAddress​(NetworkInterface anInterface,
                                                        boolean preferIPv6)
                                                 throws IOException
        Return a first InetAddress of network interface But, if any network interfaces were not found locally, null could be returned.
        Parameters:
        anInterface - the type of network interface
        preferIPv6 - if true, prefer IPv6 InetAddress. otherwise prefer IPv4 InetAddress
        Returns:
        a first found InetAddress.
        Throws:
        IOException - if an I/O error occurs or a network interface was not found
      • isLoopbackNetworkInterface

        public static boolean isLoopbackNetworkInterface​(NetworkInterface anInterface)
      • supportsMulticast

        public static boolean supportsMulticast​(NetworkInterface anInterface)
      • isBindAddressValid

        public static boolean isBindAddressValid​(String addressString)
      • resolveBindInterfaceName

        public static InetAddress resolveBindInterfaceName​(String addressString)