Search in sources :

Example 56 with DatagramChannel

use of java.nio.channels.DatagramChannel in project j2objc by google.

the class DatagramChannelTest method test_socket_IllegalBlockingModeException.

/**
 * @tests DatagramChannel#socket()
 */
public void test_socket_IllegalBlockingModeException() throws Exception {
    // regression test for Harmony-1036
    DatagramChannel channel = DatagramChannel.open();
    channel.configureBlocking(false);
    DatagramSocket socket = channel.socket();
    try {
        socket.send(null);
        fail("should throw IllegalBlockingModeException");
    } catch (IllegalBlockingModeException e) {
    // expected
    }
    try {
        socket.receive(null);
        fail("should throw IllegalBlockingModeException");
    } catch (IllegalBlockingModeException e) {
    // expected
    }
    channel.close();
}
Also used : DatagramSocket(java.net.DatagramSocket) DatagramChannel(java.nio.channels.DatagramChannel) IllegalBlockingModeException(java.nio.channels.IllegalBlockingModeException)

Example 57 with DatagramChannel

use of java.nio.channels.DatagramChannel in project j2objc by google.

the class DatagramChannelTest method test_getLocalSocketAddress_afterClose.

public void test_getLocalSocketAddress_afterClose() throws IOException {
    DatagramChannel dc = DatagramChannel.open();
    assertNull(dc.socket().getLocalSocketAddress());
    InetSocketAddress bindAddr = new InetSocketAddress("localhost", 0);
    dc.socket().bind(bindAddr);
    assertNotNull(dc.socket().getLocalSocketAddress());
    dc.close();
    assertFalse(dc.isOpen());
    dc.socket().getLocalSocketAddress();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) DatagramChannel(java.nio.channels.DatagramChannel)

Example 58 with DatagramChannel

use of java.nio.channels.DatagramChannel in project j2objc by google.

the class DatagramChannelTest method testReadWrite_NonBlock_WriterNotBound.

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();
}
Also used : DatagramChannel(java.nio.channels.DatagramChannel) ByteBuffer(java.nio.ByteBuffer)

Example 59 with DatagramChannel

use of java.nio.channels.DatagramChannel in project j2objc by google.

the class DatagramChannelTest method test_send_LBuffer_LSocketAddress_PositionNotZero.

public void test_send_LBuffer_LSocketAddress_PositionNotZero() throws Exception {
    // regression test for Harmony-701
    int CAPACITY_NORMAL = 256;
    int position = 16;
    DatagramChannel dc = DatagramChannel.open();
    byte[] sourceArray = new byte[CAPACITY_NORMAL];
    // send ByteBuffer whose position is not zero
    ByteBuffer sourceBuf = ByteBuffer.wrap(sourceArray);
    sourceBuf.position(position);
    int ret = dc.send(sourceBuf, datagramSocket1Address);
    // assert send (256 - 16) bytes
    assertEquals(CAPACITY_NORMAL - position, ret);
    // assert the position of ByteBuffer has been set
    assertEquals(CAPACITY_NORMAL, sourceBuf.position());
}
Also used : DatagramChannel(java.nio.channels.DatagramChannel) ByteBuffer(java.nio.ByteBuffer)

Example 60 with DatagramChannel

use of java.nio.channels.DatagramChannel in project j2objc by google.

the class DatagramChannelMulticastTest method test_joinAnySource_nonMulticastGroupAddress_IPv6.

public void test_joinAnySource_nonMulticastGroupAddress_IPv6() throws Exception {
    if (!supportsMulticast) {
        return;
    }
    DatagramChannel dc = createReceiverChannel();
    try {
        dc.join(UNICAST_IPv6_1, ipv6NetworkInterface);
        fail();
    } catch (IllegalArgumentException expected) {
    }
    dc.close();
}
Also used : DatagramChannel(java.nio.channels.DatagramChannel)

Aggregations

DatagramChannel (java.nio.channels.DatagramChannel)211 InetSocketAddress (java.net.InetSocketAddress)91 ByteBuffer (java.nio.ByteBuffer)71 IOException (java.io.IOException)57 DatagramSocket (java.net.DatagramSocket)21 SocketAddress (java.net.SocketAddress)21 MembershipKey (java.nio.channels.MembershipKey)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