Java HTTP Port Find isMulticastSupported(NetworkInterface pNif)

Here you can find the source of isMulticastSupported(NetworkInterface pNif)

Description

Check whether the given interface supports multicast and is up

License

Apache License

Parameter

Parameter Description
pNif check whether the given interface supports multicast

Return

true if multicast is supported and the interface is up

Declaration

public static boolean isMulticastSupported(NetworkInterface pNif) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.*;
import java.util.*;

public class Main {
    private static Method isUp;
    private static Method supportsMulticast;

    /**//from   w  ww  .  j a va  2 s .c  om
     * Check, whether multicast is supported at all by at least one interface
     *
     * @return true if at least one network interface supports multicast
     */
    public static boolean isMulticastSupported() throws SocketException {
        return getMulticastAddresses().size() != 0;
    }

    /**
     * Check whether the given interface supports multicast and is up
     *
     * @param pNif check whether the given interface supports multicast
     * @return true if multicast is supported and the interface is up
     */
    public static boolean isMulticastSupported(NetworkInterface pNif) {
        return pNif != null && checkMethod(pNif, isUp) && checkMethod(pNif, supportsMulticast);
    }

    /**
     * Check whether the given address' interface supports multicast
     *
     * @param pAddr address to check
     * @return true if the underlying networkinterface is up and supports multicast
     * @throws SocketException
     */
    public static boolean isMulticastSupported(InetAddress pAddr) throws SocketException {
        return isMulticastSupported(NetworkInterface.getByInetAddress(pAddr));
    }

    /**
     * Get all local addresses on which a multicast can be send
     *
     * @return list of all multi cast capable addresses
     */
    public static List<InetAddress> getMulticastAddresses() throws SocketException {
        Enumeration<NetworkInterface> nifs = NetworkInterface.getNetworkInterfaces();
        List<InetAddress> ret = new ArrayList<InetAddress>();
        while (nifs.hasMoreElements()) {
            NetworkInterface nif = nifs.nextElement();
            if (checkMethod(nif, supportsMulticast) && checkMethod(nif, isUp)) {
                Enumeration<InetAddress> addresses = nif.getInetAddresses();
                while (addresses.hasMoreElements()) {
                    InetAddress addr = addresses.nextElement();
                    // TODO: IpV6 support
                    if (!(addr instanceof Inet6Address)) {
                        ret.add(addr);
                    }
                }
            }
        }
        return ret;
    }

    private static Boolean checkMethod(NetworkInterface iface, Method toCheck) {
        if (toCheck != null) {
            try {
                return (Boolean) toCheck.invoke(iface, (Object[]) null);
            } catch (IllegalAccessException e) {
                return false;
            } catch (InvocationTargetException e) {
                return false;
            }
        }
        // Cannot check, hence we assume that is true
        return true;
    }
}

Related

  1. isLocalPortAvailable(final int port)
  2. isLocalPortFree(final int port)
  3. isLocalPortOccupied(int portNum)
  4. isLocalPortUsed(int port)
  5. isLoclePortUsing(int port)
  6. isOpen(final int port)
  7. isPortActive(String host, int port)
  8. isPortAvailable(final int port)
  9. isPortAvailable(int p)