Search in sources :

Example 81 with ServerSocketChannel

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

the class SocketChannelTest method test_socketChannel_write_close.

/**
     * @tests SocketChannel#write(ByteBuffer) after close
     */
public void test_socketChannel_write_close() throws Exception {
    // regression 4 for HARMONY-549
    ServerSocketChannel ssc = ServerSocketChannel.open();
    ssc.socket().bind(localAddr2);
    SocketChannel sc = SocketChannel.open();
    sc.connect(localAddr2);
    SocketChannel sock = ssc.accept();
    ByteBuffer buf = null;
    ssc.close();
    sc.close();
    try {
        sc.write(buf);
        fail("should throw NPE");
    } catch (NullPointerException e) {
    // expected
    }
    sock.close();
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) ByteBuffer(java.nio.ByteBuffer) ServerSocketChannel(java.nio.channels.ServerSocketChannel)

Example 82 with ServerSocketChannel

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

the class SinkChannelTest method test_socketChannel_read_close.

public void test_socketChannel_read_close() throws Exception {
    ServerSocketChannel ssc = ServerSocketChannel.open();
    ssc.socket().bind(new InetSocketAddress(InetAddress.getLocalHost(), 49999));
    SocketChannel sc = SocketChannel.open();
    ByteBuffer buf = null;
    try {
        sc.write(buf);
        fail("should throw NPE");
    } catch (NullPointerException e) {
    // expected
    }
    sc.connect(new InetSocketAddress(InetAddress.getLocalHost(), 49999));
    SocketChannel sock = ssc.accept();
    ssc.close();
    sc.close();
    try {
        sc.write(buf);
        fail("should throw NPE");
    } catch (NullPointerException e) {
    // expected
    }
    sock.close();
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) InetSocketAddress(java.net.InetSocketAddress) ByteBuffer(java.nio.ByteBuffer) ServerSocketChannel(java.nio.channels.ServerSocketChannel)

Example 83 with ServerSocketChannel

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

the class SocketChannelTest method testReadByteBuffer_Direct2.

public void testReadByteBuffer_Direct2() throws IOException {
    byte[] request = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    ByteBuffer buffer = ByteBuffer.allocateDirect(128);
    ServerSocketChannel server = ServerSocketChannel.open();
    server.socket().bind(new InetSocketAddress(InetAddress.getLocalHost(), 0), 5);
    Socket client = new Socket(InetAddress.getLocalHost(), server.socket().getLocalPort());
    client.setTcpNoDelay(false);
    Socket worker = server.socket().accept();
    SocketChannel workerChannel = worker.getChannel();
    OutputStream out = client.getOutputStream();
    out.write(request);
    out.close();
    buffer.limit(5);
    int bytesRead = workerChannel.read(buffer);
    assertEquals(5, bytesRead);
    assertEquals(5, buffer.position());
    buffer.limit(request.length);
    bytesRead = workerChannel.read(buffer);
    assertEquals(6, bytesRead);
    buffer.flip();
    assertEquals(request.length, buffer.limit());
    assertEquals(ByteBuffer.wrap(request), buffer);
    client.close();
    worker.close();
    server.close();
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) InetSocketAddress(java.net.InetSocketAddress) OutputStream(java.io.OutputStream) ByteBuffer(java.nio.ByteBuffer) ServerSocketChannel(java.nio.channels.ServerSocketChannel) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket)

Example 84 with ServerSocketChannel

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

the class ServerSocketChannelTest method test_accept_SOTIMEOUT.

/**
     * @tests ServerSocketChannel#socket().getSoTimeout()
     */
public void test_accept_SOTIMEOUT() throws IOException {
    // regression test for Harmony-707
    final int SO_TIMEOUT = 10;
    ServerSocketChannel sc = ServerSocketChannel.open();
    try {
        ServerSocket ss = sc.socket();
        ss.bind(localAddr1);
        sc.configureBlocking(false);
        ss.setSoTimeout(SO_TIMEOUT);
        SocketChannel client = sc.accept();
        // non blocking mode, returns null since there are no pending connections.
        assertNull(client);
        int soTimeout = ss.getSoTimeout();
        // Harmony fails here.
        assertEquals(SO_TIMEOUT, soTimeout);
    } finally {
        sc.close();
    }
}
Also used : ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) ServerSocket(java.net.ServerSocket) ServerSocketChannel(java.nio.channels.ServerSocketChannel)

Example 85 with ServerSocketChannel

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

the class SocketChannelTest method test_write$LByteBuffer_buffers.

/**
     * @tests java.nio.channels.SocketChannel#write(ByteBuffer[])
     */
public void test_write$LByteBuffer_buffers() throws IOException {
    // Set-up
    ServerSocketChannel server = ServerSocketChannel.open();
    server.socket().bind(null);
    SocketChannel client = SocketChannel.open();
    client.connect(server.socket().getLocalSocketAddress());
    SocketChannel worker = server.accept();
    // A variety of buffer types to write
    byte[] data = "Hello world!".getBytes("UTF-8");
    ByteBuffer[] buffers = new ByteBuffer[3];
    buffers[0] = ByteBuffer.wrap(data, 0, 2);
    assertFalse(buffers[0].isDirect());
    assertTrue(buffers[0].hasArray());
    buffers[1] = ByteBuffer.wrap(data, 2, 4).asReadOnlyBuffer();
    assertFalse(buffers[1].isDirect());
    assertFalse(buffers[1].hasArray());
    buffers[2] = ByteBuffer.allocateDirect(42);
    buffers[2].put(data, 6, data.length - 6);
    buffers[2].flip();
    assertTrue(buffers[2].isDirect());
    // Android's direct buffers do have a backing array.
    assertTrue(buffers[2].hasArray());
    // Write them out, read what we wrote and check it
    client.write(buffers);
    client.close();
    ByteBuffer readBuffer = ByteBuffer.allocate(1024);
    while (EOF != worker.read(readBuffer)) {
    }
    ;
    readBuffer.flip();
    assertEquals(ByteBuffer.wrap(data), readBuffer);
    // Tidy-up
    worker.close();
    server.close();
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) ByteBuffer(java.nio.ByteBuffer) ServerSocketChannel(java.nio.channels.ServerSocketChannel)

Aggregations

ServerSocketChannel (java.nio.channels.ServerSocketChannel)122 SocketChannel (java.nio.channels.SocketChannel)65 InetSocketAddress (java.net.InetSocketAddress)55 IOException (java.io.IOException)43 ByteBuffer (java.nio.ByteBuffer)26 ServerSocket (java.net.ServerSocket)19 SelectionKey (java.nio.channels.SelectionKey)19 Selector (java.nio.channels.Selector)13 Socket (java.net.Socket)12 Test (org.junit.Test)11 ClosedChannelException (java.nio.channels.ClosedChannelException)10 InetAddress (java.net.InetAddress)9 ClosedSelectorException (java.nio.channels.ClosedSelectorException)8 SocketException (java.net.SocketException)6 Benchmark (org.openjdk.jmh.annotations.Benchmark)6 ClosedByInterruptException (java.nio.channels.ClosedByInterruptException)5 URISyntaxException (java.net.URISyntaxException)4 CancelledKeyException (java.nio.channels.CancelledKeyException)4 IllegalBlockingModeException (java.nio.channels.IllegalBlockingModeException)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4