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();
}
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());
}
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();
}
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();
}
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();
}
Aggregations