Search in sources :

Example 1 with ProtocolFamily

use of java.net.ProtocolFamily in project jdk8u_jdk by JetBrains.

the class JdpClient method main.

public static void main(String[] args) {
    try {
        String discoveryPort = System.getProperty("com.sun.management.jdp.port");
        String discoveryAddress = System.getProperty("com.sun.management.jdp.address");
        if (discoveryAddress == null || discoveryPort == null) {
            System.out.println("Test failed. address and port must be specified");
            return;
        }
        int port = Integer.parseInt(discoveryPort);
        InetAddress address = InetAddress.getByName(discoveryAddress);
        ProtocolFamily family = (address instanceof Inet6Address) ? StandardProtocolFamily.INET6 : StandardProtocolFamily.INET;
        DatagramChannel channel;
        channel = DatagramChannel.open(family);
        channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
        channel.bind(new InetSocketAddress(port));
        Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
        for (NetworkInterface interf : Collections.list(nets)) {
            if (interf.supportsMulticast()) {
                try {
                    channel.join(address, interf);
                } catch (IOException e) {
                // Skip not configured interfaces
                }
            }
        }
        PacketListener listener = new PacketListener(channel);
        new Thread(listener, "Jdp Client").start();
    } catch (RuntimeException e) {
        System.out.println("Test failed.");
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("Test failed. unexpected error " + e);
    }
}
Also used : ProtocolFamily(java.net.ProtocolFamily) StandardProtocolFamily(java.net.StandardProtocolFamily) InetSocketAddress(java.net.InetSocketAddress) DatagramChannel(java.nio.channels.DatagramChannel) NetworkInterface(java.net.NetworkInterface) Inet6Address(java.net.Inet6Address) IOException(java.io.IOException) JdpException(sun.management.jdp.JdpException) IOException(java.io.IOException) InetAddress(java.net.InetAddress)

Aggregations

IOException (java.io.IOException)1 Inet6Address (java.net.Inet6Address)1 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 NetworkInterface (java.net.NetworkInterface)1 ProtocolFamily (java.net.ProtocolFamily)1 StandardProtocolFamily (java.net.StandardProtocolFamily)1 DatagramChannel (java.nio.channels.DatagramChannel)1 JdpException (sun.management.jdp.JdpException)1