Search in sources :

Example 51 with DatagramChannel

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

the class DatagramChannelTest method testSocket_BasicStatusBeforeConnect.

// -------------------------------------------------------------------
// Test for socket()
// -------------------------------------------------------------------
/**
 * Test method for 'DatagramChannelImpl.socket()'
 */
public void testSocket_BasicStatusBeforeConnect() throws Exception {
    final DatagramChannel dc = DatagramChannel.open();
    // not connected
    assertFalse(dc.isConnected());
    DatagramSocket s1 = dc.socket();
    assertFalse(s1.isBound());
    assertFalse(s1.isClosed());
    assertFalse(s1.isConnected());
    assertFalse(s1.getBroadcast());
    assertFalse(s1.getReuseAddress());
    assertNull(s1.getInetAddress());
    assertTrue(s1.getLocalAddress().isAnyLocalAddress());
    assertEquals(s1.getLocalPort(), 0);
    assertNull(s1.getLocalSocketAddress());
    assertEquals(s1.getPort(), -1);
    assertTrue(s1.getReceiveBufferSize() >= 8192);
    assertNull(s1.getRemoteSocketAddress());
    assertFalse(s1.getReuseAddress());
    assertTrue(s1.getSendBufferSize() >= 8192);
    assertEquals(s1.getSoTimeout(), 0);
    assertEquals(s1.getTrafficClass(), 0);
    DatagramSocket s2 = dc.socket();
    // same
    assertSame(s1, s2);
    dc.close();
}
Also used : DatagramSocket(java.net.DatagramSocket) DatagramChannel(java.nio.channels.DatagramChannel)

Example 52 with DatagramChannel

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

the class DatagramChannelTest method testSocket_Block_BasicStatusAfterConnect.

/**
 * Test method for 'DatagramChannelImpl.socket()'
 */
public void testSocket_Block_BasicStatusAfterConnect() throws IOException {
    final DatagramChannel dc = DatagramChannel.open();
    dc.connect(datagramSocket1Address);
    DatagramSocket s1 = dc.socket();
    assertSocketAfterConnect(s1);
    DatagramSocket s2 = dc.socket();
    // same
    assertSame(s1, s2);
    dc.close();
}
Also used : DatagramSocket(java.net.DatagramSocket) DatagramChannel(java.nio.channels.DatagramChannel)

Example 53 with DatagramChannel

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

the class DatagramChannelTest method test_read_LByteBuffer_closed_nullBuf.

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

Example 54 with DatagramChannel

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

the class DatagramChannelTest method test_bind_null.

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

Example 55 with DatagramChannel

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

the class DatagramChannelTest method testReceive_UnboundBufNotEmpty.

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

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