summaryrefslogtreecommitdiffstats
path: root/src/main/java/derms/net/Net.java
blob: f2eb1fdc9efe1912da2721d664fd0ff108f97b84 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package derms.net;

import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.NoSuchElementException;

public class Net {
    /** Return the first non-loopback multicast interface in the system, or throw exception if no such interface exists. */
    public static NetworkInterface getMulticastInterface() throws SocketException, NoSuchElementException {
        Enumeration<NetworkInterface> ifss =  NetworkInterface.getNetworkInterfaces();
        while (ifss.hasMoreElements()) {
            NetworkInterface ifs = ifss.nextElement();
            if (ifs.supportsMulticast() && !ifs.isLoopback() && ifs.isUp())
                return ifs;
        }
        throw new NoSuchElementException("no multicast interface available");
    }
}