Search in sources :

Example 1 with DatagramChannel

use of java.nio.channels.DatagramChannel in project pinpoint by naver.

the class SpanStreamUdpSender method createChannel.

private DatagramChannel createChannel(String host, int port, int timeout, int sendBufferSize) {
    DatagramChannel datagramChannel = null;
    DatagramSocket socket = null;
    try {
        datagramChannel = DatagramChannel.open();
        socket = datagramChannel.socket();
        socket.setSoTimeout(timeout);
        socket.setSendBufferSize(sendBufferSize);
        if (logger.isWarnEnabled()) {
            final int checkSendBufferSize = socket.getSendBufferSize();
            if (sendBufferSize != checkSendBufferSize) {
                logger.warn("DatagramChannel.setSendBufferSize() error. {}!={}", sendBufferSize, checkSendBufferSize);
            }
        }
        InetSocketAddress serverAddress = new InetSocketAddress(host, port);
        datagramChannel.connect(serverAddress);
        return datagramChannel;
    } catch (IOException e) {
        if (socket != null) {
            socket.close();
        }
        if (datagramChannel != null) {
            try {
                datagramChannel.close();
            } catch (IOException ignored) {
            }
        }
        throw new IllegalStateException("DatagramChannel create fail. Cause" + e.getMessage(), e);
    }
}
Also used : DatagramSocket(java.net.DatagramSocket) InetSocketAddress(java.net.InetSocketAddress) DatagramChannel(java.nio.channels.DatagramChannel) IOException(java.io.IOException)

Example 2 with DatagramChannel

use of java.nio.channels.DatagramChannel in project opennms by OpenNMS.

the class SelectorTrackerTest method test.

@Test
public void test() throws IOException {
    Selector selector = Selector.open();
    assertTrue(selector.isOpen());
    selector.close();
    assertFalse(selector.isOpen());
    DatagramChannel c = DatagramChannel.open();
    DatagramSocket s = c.socket();
    s.setSoTimeout(1000);
    byte[] buf = new byte[1024];
    DatagramPacket p = new DatagramPacket(buf, 1024, InetAddress.getLocalHost(), 7);
    s.send(p);
}
Also used : DatagramSocket(java.net.DatagramSocket) DatagramPacket(java.net.DatagramPacket) DatagramChannel(java.nio.channels.DatagramChannel) Selector(java.nio.channels.Selector) Test(org.junit.Test)

Example 3 with DatagramChannel

use of java.nio.channels.DatagramChannel in project voltdb by VoltDB.

the class ExportBenchmark method setupSocketListener.

/**
     * Sets up a UDP socket on a certain port to listen for connections.
     */
private void setupSocketListener() {
    DatagramChannel channel = null;
    // Setup Listener
    try {
        statsSocketSelector = SelectorProvider.provider().openSelector();
        channel = DatagramChannel.open();
        channel.configureBlocking(false);
    } catch (IOException e) {
        exitWithException("Couldn't set up network channels", e);
    }
    // Bind to port & register with a channel
    try {
        InetSocketAddress isa = new InetSocketAddress(CoreUtils.getLocalAddress(), config.statsPort);
        channel.socket().setReuseAddress(true);
        channel.socket().bind(isa);
        channel.register(statsSocketSelector, SelectionKey.OP_READ);
    } catch (IOException e) {
        exitWithException("Couldn't bind to socket", e);
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) DatagramChannel(java.nio.channels.DatagramChannel) IOException(java.io.IOException)

Example 4 with DatagramChannel

use of java.nio.channels.DatagramChannel 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)

Example 5 with DatagramChannel

use of java.nio.channels.DatagramChannel in project robovm by robovm.

the class DatagramChannelTest method test_read_LByteBuffer_NotConnected_nullBuf.

/**
     * @tests DatagramChannel#read(ByteBuffer)
     */
public void test_read_LByteBuffer_NotConnected_nullBuf() throws Exception {
    // regression test for Harmony-754
    ByteBuffer c = null;
    DatagramChannel channel = DatagramChannel.open();
    try {
        channel.read(c);
        fail("Should throw NullPointerException");
    } catch (NullPointerException e) {
    // expected
    }
}
Also used : DatagramChannel(java.nio.channels.DatagramChannel) ByteBuffer(java.nio.ByteBuffer)

Aggregations

DatagramChannel (java.nio.channels.DatagramChannel)214 InetSocketAddress (java.net.InetSocketAddress)92 ByteBuffer (java.nio.ByteBuffer)71 IOException (java.io.IOException)58 MembershipKey (java.nio.channels.MembershipKey)22 DatagramSocket (java.net.DatagramSocket)21 SocketAddress (java.net.SocketAddress)21 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)20 SocketChannel (java.nio.channels.SocketChannel)17 SelectionKey (java.nio.channels.SelectionKey)16 InetAddress (java.net.InetAddress)13 Selector (java.nio.channels.Selector)13 Test (org.junit.Test)11 SocketException (java.net.SocketException)9 ClosedChannelException (java.nio.channels.ClosedChannelException)9 Histogram (org.HdrHistogram.Histogram)8 CancelledKeyException (java.nio.channels.CancelledKeyException)7 DatagramPacket (java.net.DatagramPacket)5 NetworkInterface (java.net.NetworkInterface)5 ArrayList (java.util.ArrayList)5