Search in sources :

Example 16 with AsynchronousServerSocketChannel

use of java.nio.channels.AsynchronousServerSocketChannel in project JavaBase by SkyScraperTwc.

the class AIOTcpServer method main.

public static void main(String[] args) throws IOException, InterruptedException {
    System.out.println("main thread id: " + Thread.currentThread().getId());
    AsynchronousServerSocketChannel serverSocketChannel = AsynchronousServerSocketChannel.open().bind(new InetSocketAddress(PORT));
    serverSocketChannel.accept(serverSocketChannel, new AcceptCompletionHandler());
    // keep the main thread out of exiting
    Thread t = new Thread(new Runnable() {

        @Override
        public void run() {
            System.out.println("Listening on port: " + PORT);
            while (true) ;
        }
    });
    t.start();
    t.join();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) AsynchronousServerSocketChannel(java.nio.channels.AsynchronousServerSocketChannel)

Example 17 with AsynchronousServerSocketChannel

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

the class AsynchronousServerSocketChannelTest method test_supportedOptions.

public void test_supportedOptions() throws Throwable {
    AsynchronousServerSocketChannel assc = AsynchronousServerSocketChannel.open();
    Set<SocketOption<?>> supportedOptions = assc.supportedOptions();
    assertEquals(2, supportedOptions.size());
    assertTrue(supportedOptions.contains(StandardSocketOptions.SO_REUSEADDR));
    assertTrue(supportedOptions.contains(StandardSocketOptions.SO_RCVBUF));
    // supportedOptions should work after close according to spec
    assc.close();
    supportedOptions = assc.supportedOptions();
    assertEquals(2, supportedOptions.size());
}
Also used : SocketOption(java.net.SocketOption) AsynchronousServerSocketChannel(java.nio.channels.AsynchronousServerSocketChannel)

Example 18 with AsynchronousServerSocketChannel

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

the class AsynchronousServerSocketChannelTest method test_close.

public void test_close() throws Throwable {
    AsynchronousServerSocketChannel assc = AsynchronousServerSocketChannel.open();
    assc.bind(new InetSocketAddress(0));
    assc.close();
    Future<AsynchronousSocketChannel> acceptFuture = assc.accept();
    try {
        acceptFuture.get(1000, TimeUnit.MILLISECONDS);
        fail();
    } catch (ExecutionException expected) {
        assertTrue(expected.getCause() instanceof ClosedChannelException);
    }
    FutureLikeCompletionHandler<AsynchronousSocketChannel> acceptCompletionHandler = new FutureLikeCompletionHandler<AsynchronousSocketChannel>();
    assc.accept(null, /* attachment */
    acceptCompletionHandler);
    try {
        acceptCompletionHandler.get(1000);
        fail();
    } catch (ClosedChannelException expected) {
    }
    try {
        assc.bind(new InetSocketAddress(0));
        fail();
    } catch (ClosedChannelException expected) {
    }
    try {
        assc.setOption(StandardSocketOptions.SO_REUSEADDR, true);
        fail();
    } catch (ClosedChannelException expected) {
    }
    // Try second close
    assc.close();
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) AsynchronousSocketChannel(java.nio.channels.AsynchronousSocketChannel) InetSocketAddress(java.net.InetSocketAddress) AsynchronousServerSocketChannel(java.nio.channels.AsynchronousServerSocketChannel) ExecutionException(java.util.concurrent.ExecutionException)

Example 19 with AsynchronousServerSocketChannel

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

the class AsynchronousServerSocketChannelTest method test_options_iae.

public void test_options_iae() throws Throwable {
    AsynchronousServerSocketChannel assc = AsynchronousServerSocketChannel.open();
    try {
        assc.setOption(StandardSocketOptions.SO_KEEPALIVE, true);
        fail();
    } catch (UnsupportedOperationException expected) {
    }
    assc.close();
}
Also used : AsynchronousServerSocketChannel(java.nio.channels.AsynchronousServerSocketChannel)

Example 20 with AsynchronousServerSocketChannel

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

the class AsynchronousServerSocketChannelTest method test_options.

public void test_options() throws Throwable {
    AsynchronousServerSocketChannel assc = AsynchronousServerSocketChannel.open();
    assc.setOption(StandardSocketOptions.SO_RCVBUF, 5000);
    assertEquals(5000, (long) assc.getOption(StandardSocketOptions.SO_RCVBUF));
    assc.setOption(StandardSocketOptions.SO_REUSEADDR, true);
    assertTrue(assc.getOption(StandardSocketOptions.SO_REUSEADDR));
    assc.close();
}
Also used : AsynchronousServerSocketChannel(java.nio.channels.AsynchronousServerSocketChannel)

Aggregations

AsynchronousServerSocketChannel (java.nio.channels.AsynchronousServerSocketChannel)21 InetSocketAddress (java.net.InetSocketAddress)15 AsynchronousSocketChannel (java.nio.channels.AsynchronousSocketChannel)12 AlreadyBoundException (java.nio.channels.AlreadyBoundException)4 ExecutionException (java.util.concurrent.ExecutionException)4 ServerSocket (java.net.ServerSocket)3 Socket (java.net.Socket)3 ClosedChannelException (java.nio.channels.ClosedChannelException)3 NotYetBoundException (java.nio.channels.NotYetBoundException)3 UnresolvedAddressException (java.nio.channels.UnresolvedAddressException)3 IOException (java.io.IOException)2 BindException (java.net.BindException)2 ByteBuffer (java.nio.ByteBuffer)2 AsynchronousChannelGroup (java.nio.channels.AsynchronousChannelGroup)2 AsynchronousCloseException (java.nio.channels.AsynchronousCloseException)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Inet6Address (java.net.Inet6Address)1 SocketOption (java.net.SocketOption)1 AsynchronousChannelProvider (java.nio.channels.spi.AsynchronousChannelProvider)1 Path (java.nio.file.Path)1