Class NetworkUtils


  • public final class NetworkUtils
    extends java.lang.Object
    Utilities for networking.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String getCanonicalHostName()
      Returns the fully-qualified domain name of the local host in all lower case.
      static java.net.InetAddress getExternalAddressOfLocalSystem()
      Returns the externally-facing IPv4 network address of the local host.
      static int pickUnusedPort()
      Returns random unused local port that can be used for TCP listening server.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • pickUnusedPort

        public static int pickUnusedPort()
        Returns random unused local port that can be used for TCP listening server.
        Throws:
        java.lang.RuntimeException - if failed to find free port after 16 attempts
      • getCanonicalHostName

        public static java.lang.String getCanonicalHostName()
        Returns the fully-qualified domain name of the local host in all lower case.
        Throws:
        java.lang.RuntimeException - to wrap UnknownHostException if the local host could not be resolved into an address
      • getExternalAddressOfLocalSystem

        public static java.net.InetAddress getExternalAddressOfLocalSystem()
                                                                    throws java.net.UnknownHostException
        Returns the externally-facing IPv4 network address of the local host.

        This function implements a workaround for an issue in InetAddress.getLocalHost().

        Note: This code was pilfered from com.google.net.base.LocalHost which was never made open source.

        Throws:
        java.net.UnknownHostException - if the local host could not be resolved into an address