Search in sources :

Example 86 with ServerSocketChannel

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

the class SocketChannelTest method test_writev.

/**
     * @tests java.nio.channels.SocketChannel#write(ByteBuffer[])
     */
public void test_writev() throws Exception {
    ServerSocketChannel ssc = ServerSocketChannel.open();
    ssc.socket().bind(localAddr2);
    SocketChannel sc = SocketChannel.open();
    sc.connect(localAddr2);
    SocketChannel sock = ssc.accept();
    ByteBuffer[] buf = { ByteBuffer.allocate(10), ByteBuffer.allocateDirect(20) };
    while (buf[0].remaining() != 0 && buf[1].remaining() != 0) {
        assertTrue(sc.write(buf, 0, 2) >= 0);
    }
    ByteBuffer target = ByteBuffer.allocate(30);
    while (target.remaining() != 0) {
        assertTrue(sock.read(target) >= 0);
    }
    ssc.close();
    sc.close();
    sock.close();
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) ByteBuffer(java.nio.ByteBuffer) ServerSocketChannel(java.nio.channels.ServerSocketChannel)

Example 87 with ServerSocketChannel

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

the class OldServerSocketTest method test_accept.

public void test_accept() throws IOException {
    ServerSocket newSocket = new ServerSocket(0);
    newSocket.setSoTimeout(500);
    try {
        Socket accepted = newSocket.accept();
        fail("SocketTimeoutException was not thrown: " + accepted);
    } catch (SocketTimeoutException expected) {
    }
    newSocket.close();
    ServerSocketChannel ssc = ServerSocketChannel.open();
    ServerSocket ss = ssc.socket();
    try {
        ss.accept();
        fail("IllegalBlockingModeException was not thrown.");
    } catch (IllegalBlockingModeException ibme) {
    //expected
    } finally {
        ss.close();
        ssc.close();
    }
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) ServerSocket(java.net.ServerSocket) IllegalBlockingModeException(java.nio.channels.IllegalBlockingModeException) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) ServerSocketChannel(java.nio.channels.ServerSocketChannel)

Example 88 with ServerSocketChannel

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

the class SelectorTest method testNonBlockingConnect_immediate.

public void testNonBlockingConnect_immediate() throws Exception {
    // Test the case where we [probably] connect immediately.
    Selector selector = Selector.open();
    ServerSocketChannel ssc = ServerSocketChannel.open();
    try {
        ssc.configureBlocking(false);
        ssc.socket().bind(null);
        SocketChannel sc = SocketChannel.open();
        sc.configureBlocking(false);
        sc.connect(ssc.socket().getLocalSocketAddress());
        SelectionKey key = sc.register(selector, SelectionKey.OP_CONNECT);
        assertEquals(1, selector.select());
        assertEquals(SelectionKey.OP_CONNECT, key.readyOps());
        sc.finishConnect();
    } finally {
        selector.close();
        ssc.close();
    }
}
Also used : ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) SelectionKey(java.nio.channels.SelectionKey) ServerSocketChannel(java.nio.channels.ServerSocketChannel) Selector(java.nio.channels.Selector)

Example 89 with ServerSocketChannel

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

the class ServerSocketChannelTest method testNonBlockingAccept.

// http://code.google.com/p/android/issues/detail?id=16579
public void testNonBlockingAccept() throws Exception {
    ServerSocketChannel ssc = ServerSocketChannel.open();
    try {
        ssc.configureBlocking(false);
        ssc.socket().bind(null);
        // Should return immediately, since we're non-blocking.
        assertNull(ssc.accept());
    } finally {
        ssc.close();
    }
}
Also used : ServerSocketChannel(java.nio.channels.ServerSocketChannel)

Example 90 with ServerSocketChannel

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

the class ChannelsTest method test_newReader_LReadableByteChannel_LString.

/**
	 * @tests java.nio.channels.Channels#newReader(ReadableByteChannel channel,
	 *        String charsetName)
	 */
public void test_newReader_LReadableByteChannel_LString() throws IOException {
    InetSocketAddress localAddr = new InetSocketAddress("127.0.0.1", Support_PortManager.getNextPort());
    ServerSocketChannel ssc = ServerSocketChannel.open();
    ssc.socket().bind(localAddr);
    SocketChannel sc = SocketChannel.open();
    sc.connect(localAddr);
    sc.configureBlocking(false);
    assertFalse(sc.isBlocking());
    ssc.accept().close();
    ssc.close();
    assertFalse(sc.isBlocking());
    Reader reader = Channels.newReader(sc, "UTF16");
    try {
        int i = reader.read();
        fail("should throw IllegalBlockingModeException");
    } catch (IllegalBlockingModeException expected) {
    }
    try {
        Channels.newInputStream(sc).read();
        fail("should throw IllegalBlockingModeException");
    } catch (IllegalBlockingModeException expected) {
    }
    sc.close();
}
Also used : ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) InetSocketAddress(java.net.InetSocketAddress) Reader(java.io.Reader) IllegalBlockingModeException(java.nio.channels.IllegalBlockingModeException) ServerSocketChannel(java.nio.channels.ServerSocketChannel)

Aggregations

ServerSocketChannel (java.nio.channels.ServerSocketChannel)114 SocketChannel (java.nio.channels.SocketChannel)62 InetSocketAddress (java.net.InetSocketAddress)52 IOException (java.io.IOException)41 ByteBuffer (java.nio.ByteBuffer)26 SelectionKey (java.nio.channels.SelectionKey)19 ServerSocket (java.net.ServerSocket)18 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 CancelledKeyException (java.nio.channels.CancelledKeyException)5 ClosedByInterruptException (java.nio.channels.ClosedByInterruptException)5 URISyntaxException (java.net.URISyntaxException)4 IllegalBlockingModeException (java.nio.channels.IllegalBlockingModeException)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4