Search in sources :

Example 6 with AsynchronousServerSocketChannel

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

the class AsynchronousServerSocketChannelTest method test_completionHandlerAccept_nyb.

public void test_completionHandlerAccept_nyb() throws Throwable {
    AsynchronousServerSocketChannel assc = AsynchronousServerSocketChannel.open();
    FutureLikeCompletionHandler<AsynchronousSocketChannel> acceptCompletionHandler = new FutureLikeCompletionHandler<AsynchronousSocketChannel>();
    try {
        assc.accept(null, /* attachment */
        acceptCompletionHandler);
        fail();
    } catch (NotYetBoundException expected) {
    }
    assc.close();
}
Also used : NotYetBoundException(java.nio.channels.NotYetBoundException) AsynchronousSocketChannel(java.nio.channels.AsynchronousSocketChannel) AsynchronousServerSocketChannel(java.nio.channels.AsynchronousServerSocketChannel)

Example 7 with AsynchronousServerSocketChannel

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

the class AsynchronousServerSocketChannelTest method test_group.

public void test_group() throws Throwable {
    AsynchronousChannelProvider provider = AsynchronousChannelProvider.provider();
    AsynchronousChannelGroup group = provider.openAsynchronousChannelGroup(2, Executors.defaultThreadFactory());
    AsynchronousServerSocketChannel assc = AsynchronousServerSocketChannel.open(group);
    assertNull(assc.getLocalAddress());
    assc.bind(new InetSocketAddress(0));
    assertNotNull(assc.getLocalAddress());
    assertEquals(provider, assc.provider());
    assc.close();
}
Also used : AsynchronousChannelProvider(java.nio.channels.spi.AsynchronousChannelProvider) InetSocketAddress(java.net.InetSocketAddress) AsynchronousChannelGroup(java.nio.channels.AsynchronousChannelGroup) AsynchronousServerSocketChannel(java.nio.channels.AsynchronousServerSocketChannel)

Example 8 with AsynchronousServerSocketChannel

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

the class AsynchronousServerSocketChannelTest method test_futureAccept.

/* j2objc: b/162760509
    public void test_bind_used() throws Throwable {
        AsynchronousServerSocketChannel assc = AsynchronousServerSocketChannel.open();
        ServerSocket ss = new ServerSocket(0);
        try {
            assc.bind(ss.getLocalSocketAddress());
            fail();
        } catch (BindException expected) {}
        assertNull(assc.getLocalAddress());

        ss.close();
        assc.close();
    }
    */
public void test_futureAccept() throws Throwable {
    AsynchronousServerSocketChannel assc = AsynchronousServerSocketChannel.open();
    assc.bind(new InetSocketAddress(0));
    Future<AsynchronousSocketChannel> acceptFuture = assc.accept();
    Socket s = new Socket();
    s.connect(assc.getLocalAddress());
    AsynchronousSocketChannel asc = acceptFuture.get(1000, TimeUnit.MILLISECONDS);
    assertTrue(s.isConnected());
    assertNotNull(asc.getLocalAddress());
    assertEquals(asc.getLocalAddress(), s.getRemoteSocketAddress());
    assertNotNull(asc.getRemoteAddress());
    assertEquals(asc.getRemoteAddress(), s.getLocalSocketAddress());
    asc.close();
    s.close();
    assc.close();
}
Also used : AsynchronousSocketChannel(java.nio.channels.AsynchronousSocketChannel) InetSocketAddress(java.net.InetSocketAddress) AsynchronousServerSocketChannel(java.nio.channels.AsynchronousServerSocketChannel) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket)

Example 9 with AsynchronousServerSocketChannel

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

the class AsynchronousServerSocketChannelTest method test_completionHandlerAccept_attachment.

public void test_completionHandlerAccept_attachment() throws Throwable {
    AsynchronousServerSocketChannel assc = AsynchronousServerSocketChannel.open();
    assc.bind(new InetSocketAddress(0));
    FutureLikeCompletionHandler<AsynchronousSocketChannel> acceptCompletionHandler = new FutureLikeCompletionHandler<AsynchronousSocketChannel>();
    Integer attachment = new Integer(123);
    assc.accept(attachment, acceptCompletionHandler);
    Socket s = new Socket();
    s.connect(assc.getLocalAddress());
    AsynchronousSocketChannel asc = acceptCompletionHandler.get(1000);
    assertNotNull(asc);
    assertTrue(s.isConnected());
    assertEquals(attachment, acceptCompletionHandler.getAttachment());
    asc.close();
    s.close();
    assc.close();
}
Also used : AsynchronousSocketChannel(java.nio.channels.AsynchronousSocketChannel) InetSocketAddress(java.net.InetSocketAddress) AsynchronousServerSocketChannel(java.nio.channels.AsynchronousServerSocketChannel) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket)

Example 10 with AsynchronousServerSocketChannel

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

the class AsynchronousServerSocketChannelTest method test_bind_null.

public void test_bind_null() throws Throwable {
    AsynchronousServerSocketChannel assc = AsynchronousServerSocketChannel.open();
    assertTrue(assc.isOpen());
    assertNull(assc.getLocalAddress());
    assc.bind(null);
    assertNotNull(assc.getLocalAddress());
    try {
        assc.bind(null);
        fail();
    } catch (AlreadyBoundException expected) {
    }
    assc.close();
    assertFalse(assc.isOpen());
}
Also used : AlreadyBoundException(java.nio.channels.AlreadyBoundException) 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