Java Code Examples for java.nio.channels.DatagramChannel#close()

The following examples show how to use java.nio.channels.DatagramChannel#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UseDGWithIPv6.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException
{
    ByteBuffer data = ByteBuffer.wrap("TESTING DATA".getBytes());
    DatagramChannel dgChannel = DatagramChannel.open();

    for(int i = 0; i < targets.length; i++){
        data.rewind();
        SocketAddress sa = new InetSocketAddress(targets[i], port);
        System.out.println("-------------\nDG_Sending data:" +
                           "\n    remaining:" + data.remaining() +
                           "\n     position:" + data.position() +
                           "\n        limit:" + data.limit() +
                           "\n     capacity:" + data.capacity() +
                           " bytes on DG channel to " + sa);
        try {
            int n = dgChannel.send(data, sa);
            System.out.println("DG_Sent " + n + " bytes");
        } catch (IOException e) {
            //This regression test is to check vm crash only, so ioe is OK.
            e.printStackTrace();
        }
    }
    dgChannel.close();
}
 
Example 2
Source File: UseDGWithIPv6.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException
{
    ByteBuffer data = ByteBuffer.wrap("TESTING DATA".getBytes());
    DatagramChannel dgChannel = DatagramChannel.open();

    for(int i = 0; i < targets.length; i++){
        data.rewind();
        SocketAddress sa = new InetSocketAddress(targets[i], port);
        System.out.println("-------------\nDG_Sending data:" +
                           "\n    remaining:" + data.remaining() +
                           "\n     position:" + data.position() +
                           "\n        limit:" + data.limit() +
                           "\n     capacity:" + data.capacity() +
                           " bytes on DG channel to " + sa);
        try {
            int n = dgChannel.send(data, sa);
            System.out.println("DG_Sent " + n + " bytes");
        } catch (IOException e) {
            //This regression test is to check vm crash only, so ioe is OK.
            e.printStackTrace();
        }
    }
    dgChannel.close();
}
 
Example 3
Source File: DatagramChannelTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public void testReadWrite_NonBlock_WriterNotBound() throws Exception {
    byte[] sourceArray = new byte[CAPACITY_NORMAL];
    byte[] targetArray = new byte[CAPACITY_NORMAL];
    for (int i = 0; i < sourceArray.length; i++) {
        sourceArray[i] = (byte) i;
    }

    DatagramChannel dc = DatagramChannel.open();
    // The writer isn't bound, but is connected.
    dc.connect(channel1Address);
    dc.configureBlocking(false);
    channel2.configureBlocking(false);

    // write
    ByteBuffer sourceBuf = ByteBuffer.wrap(sourceArray);
    assertEquals(CAPACITY_NORMAL, dc.write(sourceBuf));

    // Connect channel2 after data has been written.
    channel2.connect(dc.socket().getLocalSocketAddress());

    // read
    ByteBuffer targetBuf = ByteBuffer.wrap(targetArray);
    assertEquals(0, this.channel2.read(targetBuf));

    dc.close();
}
 
Example 4
Source File: NioDatagramConnector.java    From neoscada with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected DatagramChannel newHandle(SocketAddress localAddress) throws Exception {
    DatagramChannel ch = DatagramChannel.open();

    try {
        if (localAddress != null) {
            ch.socket().bind(localAddress);
        }

        return ch;
    } catch (Exception e) {
        // If we got an exception while binding the datagram,
        // we have to close it otherwise we will loose an handle
        ch.close();
        throw e;
    }
}
 
Example 5
Source File: DatagramChannelTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public void test_bounded_harmony6493() throws IOException {
    DatagramChannel server = DatagramChannel.open();
    InetSocketAddress addr = new InetSocketAddress("localhost", 0);
    server.socket().bind(addr);
    SocketAddress boundedAddress = server.socket().getLocalSocketAddress();

    DatagramChannel client = DatagramChannel.open();
    ByteBuffer sent = ByteBuffer.allocate(1024);
    sent.put("test".getBytes());
    sent.flip();
    client.send(sent, boundedAddress);
    assertTrue(client.socket().isBound());

    server.close();
    client.close();
}
 
Example 6
Source File: GraylogUdpConnPool.java    From xian with Apache License 2.0 5 votes vote down vote up
public static void destroy() {
    if (singleton != null) {
        for (DatagramChannel channel : singleton.channels) {
            try {
                channel.close();
            } catch (IOException e) {
                LOG.error(e);
            }
        }
    } else {
        LOG.warn("没有开启,不需要销毁");
    }
}
 
Example 7
Source File: NotBound.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
static void wakeupWhenBound(final DatagramChannel dc) {
    Runnable wakeupTask = new Runnable() {
        public void run() {
            try {
                // poll for local address
                InetSocketAddress local;
                do {
                    Thread.sleep(50);
                    local = (InetSocketAddress)dc.getLocalAddress();
                } while (local == null);

                // send message to channel to wakeup receiver
                DatagramChannel sender = DatagramChannel.open();
                try {
                    ByteBuffer bb = ByteBuffer.wrap("hello".getBytes());
                    InetAddress lh = InetAddress.getLocalHost();
                    SocketAddress target =
                        new InetSocketAddress(lh, local.getPort());
                    sender.send(bb, target);
                } finally {
                    sender.close();
                }

            } catch (Exception x) {
                x.printStackTrace();
            }
        }};
    new Thread(wakeupTask).start();
}
 
Example 8
Source File: DatagramChannelTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void test_bind_failure() throws Exception {
    DatagramChannel dc = DatagramChannel.open();
    try {
        // Bind to a local address that is in use
        dc.socket().bind(channel1Address);
        fail();
    } catch (IOException expected) {
    } finally {
        dc.close();
    }
}
 
Example 9
Source File: NotBound.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
static void wakeupWhenBound(final DatagramChannel dc) {
    Runnable wakeupTask = new Runnable() {
        public void run() {
            try {
                // poll for local address
                InetSocketAddress local;
                do {
                    Thread.sleep(50);
                    local = (InetSocketAddress)dc.getLocalAddress();
                } while (local == null);

                // send message to channel to wakeup receiver
                DatagramChannel sender = DatagramChannel.open();
                try {
                    ByteBuffer bb = ByteBuffer.wrap("hello".getBytes());
                    InetAddress lh = InetAddress.getLocalHost();
                    SocketAddress target =
                        new InetSocketAddress(lh, local.getPort());
                    sender.send(bb, target);
                } finally {
                    sender.close();
                }

            } catch (Exception x) {
                x.printStackTrace();
            }
        }};
    new Thread(wakeupTask).start();
}
 
Example 10
Source File: DatagramChannelTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void test_bind_null() throws Exception {
    DatagramChannel dc = DatagramChannel.open();
    try {
        assertNull(dc.socket().getLocalSocketAddress());

        dc.socket().bind(null);

        InetSocketAddress localAddress = (InetSocketAddress) dc.socket().getLocalSocketAddress();
        assertTrue(localAddress.getAddress().isAnyLocalAddress());
        assertTrue(localAddress.getPort() > 0);
    } finally {
        dc.close();
    }
}
 
Example 11
Source File: ChangingAddress.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    InetAddress lh = InetAddress.getLocalHost();
    SocketAddress remote = new InetSocketAddress(lh, 1234);

    DatagramSocket ds = null;
    DatagramChannel dc = null;
    try {

        ds = new DatagramSocket();
        dc = DatagramChannel.open().bind(new InetSocketAddress(0));
        check(ds, dc);

        ds.connect(remote);
        dc.connect(remote);
        check(ds, dc);

        ds.disconnect();
        dc.disconnect();
        check(ds, dc);

        // repeat tests using socket adapter
        ds.connect(remote);
        dc.socket().connect(remote);
        check(ds, dc);

        ds.disconnect();
        dc.socket().disconnect();
        check(ds, dc);

   } finally {
        if (ds != null) ds.close();
        if (dc != null) dc.close();
   }
}
 
Example 12
Source File: ChangingAddress.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    InetAddress lh = InetAddress.getLocalHost();
    SocketAddress remote = new InetSocketAddress(lh, 1234);

    DatagramSocket ds = null;
    DatagramChannel dc = null;
    try {

        ds = new DatagramSocket();
        dc = DatagramChannel.open().bind(new InetSocketAddress(0));
        check(ds, dc);

        ds.connect(remote);
        dc.connect(remote);
        check(ds, dc);

        ds.disconnect();
        dc.disconnect();
        check(ds, dc);

        // repeat tests using socket adapter
        ds.connect(remote);
        dc.socket().connect(remote);
        check(ds, dc);

        ds.disconnect();
        dc.socket().disconnect();
        check(ds, dc);

   } finally {
        if (ds != null) ds.close();
        if (dc != null) dc.close();
   }
}
 
Example 13
Source File: TestNonBlockingNIO2.java    From code with Apache License 2.0 5 votes vote down vote up
@Test
public void send() throws IOException {
    DatagramChannel dc = DatagramChannel.open();
    dc.configureBlocking(false);
    ByteBuffer buf = ByteBuffer.allocate(1024);
    Scanner scan = new Scanner(System.in);
    while (scan.hasNext()) {
        String str = scan.next();
        buf.put((new Date().toString() + "\n" + str).getBytes());
        buf.flip();
        dc.send(buf, new InetSocketAddress("127.0.0.1", 9898));
        buf.clear();
    }
    dc.close();
}
 
Example 14
Source File: NotBound.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
static void wakeupWhenBound(final DatagramChannel dc) {
    Runnable wakeupTask = new Runnable() {
        public void run() {
            try {
                // poll for local address
                InetSocketAddress local;
                do {
                    Thread.sleep(50);
                    local = (InetSocketAddress)dc.getLocalAddress();
                } while (local == null);

                // send message to channel to wakeup receiver
                DatagramChannel sender = DatagramChannel.open();
                try {
                    ByteBuffer bb = ByteBuffer.wrap("hello".getBytes());
                    InetAddress lh = InetAddress.getLocalHost();
                    SocketAddress target =
                        new InetSocketAddress(lh, local.getPort());
                    sender.send(bb, target);
                } finally {
                    sender.close();
                }

            } catch (Exception x) {
                x.printStackTrace();
            }
        }};
    new Thread(wakeupTask).start();
}
 
Example 15
Source File: ChangingAddress.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    InetAddress lh = InetAddress.getLocalHost();
    SocketAddress remote = new InetSocketAddress(lh, 1234);

    DatagramSocket ds = null;
    DatagramChannel dc = null;
    try {

        ds = new DatagramSocket();
        dc = DatagramChannel.open().bind(new InetSocketAddress(0));
        check(ds, dc);

        ds.connect(remote);
        dc.connect(remote);
        check(ds, dc);

        ds.disconnect();
        dc.disconnect();
        check(ds, dc);

        // repeat tests using socket adapter
        ds.connect(remote);
        dc.socket().connect(remote);
        check(ds, dc);

        ds.disconnect();
        dc.socket().disconnect();
        check(ds, dc);

   } finally {
        if (ds != null) ds.close();
        if (dc != null) dc.close();
   }
}
 
Example 16
Source File: NotBound.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
static void wakeupWhenBound(final DatagramChannel dc) {
    Runnable wakeupTask = new Runnable() {
        public void run() {
            try {
                // poll for local address
                InetSocketAddress local;
                do {
                    Thread.sleep(50);
                    local = (InetSocketAddress)dc.getLocalAddress();
                } while (local == null);

                // send message to channel to wakeup receiver
                DatagramChannel sender = DatagramChannel.open();
                try {
                    ByteBuffer bb = ByteBuffer.wrap("hello".getBytes());
                    InetAddress lh = InetAddress.getLocalHost();
                    SocketAddress target =
                        new InetSocketAddress(lh, local.getPort());
                    sender.send(bb, target);
                } finally {
                    sender.close();
                }

            } catch (Exception x) {
                x.printStackTrace();
            }
        }};
    new Thread(wakeupTask).start();
}
 
Example 17
Source File: DatagramChannelTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testReceive_UnboundBufNotEmpty() throws Exception {
    DatagramChannel dc = DatagramChannel.open();
    assertFalse(dc.isConnected());
    assertFalse(dc.socket().isBound());

    ByteBuffer dst = ByteBuffer.allocateDirect(CAPACITY_NORMAL);
    // buf is not empty
    dst.put((byte) 88);
    assertEquals(dst.position() + CAPACITY_NORMAL - 1, dst.limit());
    assertNull(dc.receive(dst));

    dc.close();
}
 
Example 18
Source File: DatagramChannelTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void test_bind_unresolvedAddress() throws IOException {
    DatagramChannel dc = DatagramChannel.open();
    try {
        dc.socket().bind(new InetSocketAddress("unresolvedname", 31415));
        fail();
    } catch (IOException expected) {
    }

    assertTrue(dc.isOpen());
    assertFalse(dc.isConnected());

    dc.close();
}
 
Example 19
Source File: NotBound.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
static void wakeupWhenBound(final DatagramChannel dc) {
    Runnable wakeupTask = new Runnable() {
        public void run() {
            try {
                // poll for local address
                InetSocketAddress local;
                do {
                    Thread.sleep(50);
                    local = (InetSocketAddress)dc.getLocalAddress();
                } while (local == null);

                // send message to channel to wakeup receiver
                DatagramChannel sender = DatagramChannel.open();
                try {
                    ByteBuffer bb = ByteBuffer.wrap("hello".getBytes());
                    InetAddress lh = InetAddress.getLocalHost();
                    SocketAddress target =
                        new InetSocketAddress(lh, local.getPort());
                    sender.send(bb, target);
                } finally {
                    sender.close();
                }

            } catch (Exception x) {
                x.printStackTrace();
            }
        }};
    new Thread(wakeupTask).start();
}
 
Example 20
Source File: NotBound.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
static void wakeupWhenBound(final DatagramChannel dc) {
    Runnable wakeupTask = new Runnable() {
        public void run() {
            try {
                // poll for local address
                InetSocketAddress local;
                do {
                    Thread.sleep(50);
                    local = (InetSocketAddress)dc.getLocalAddress();
                } while (local == null);

                // send message to channel to wakeup receiver
                DatagramChannel sender = DatagramChannel.open();
                try {
                    ByteBuffer bb = ByteBuffer.wrap("hello".getBytes());
                    InetAddress lh = InetAddress.getLocalHost();
                    SocketAddress target =
                        new InetSocketAddress(lh, local.getPort());
                    sender.send(bb, target);
                } finally {
                    sender.close();
                }

            } catch (Exception x) {
                x.printStackTrace();
            }
        }};
    new Thread(wakeupTask).start();
}