code: private static Inet4Address getIPv4Address(String iface)
            throws SocketException, UnsupportedAddressTypeException,
            UnknownHostException {
      if (iface != null) {
        Enumeration<InetAddress> addresses =
                NetworkInterface.getByName(iface).getInetAddresses();
        while (addresses.hasMoreElements()) {
          InetAddress addr = addresses.nextElement();
          if (addr instanceof Inet4Address) {
            return (Inet4Address) addr;
          }
        }
      }
  
      InetAddress localhost = InetAddress.getLocalHost();
      if (localhost instanceof Inet4Address) {
        return (Inet4Address) localhost;
      }
  
      throw new UnsupportedAddressTypeException();
    }
docs: Returns a usable {@link Inet4Address} for the given interface name.
  
  <p>
  If an interface name is given, return the first usable IPv4 address for
  that interface. If no interface name is given or if that interface
  doesn't have an IPv4 address, return's localhost address (if IPv4).
  </p>
  
  <p>
  It is understood this makes the client IPv4 only, but it is important to
  remember that most BitTorrent extensions (like compact peer lists from
  trackers and UDP tracker support) are IPv4-only anyway.
  </p>
  
  @param iface The network interface name.
  @return A usable IPv4 address as a {@link Inet4Address}.
  @throws UnsupportedAddressTypeException If no IPv4 address was available
  to bind on.