Search in sources :

Example 21 with ServerSocketChannel

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

the class SocketChannelTest method test_socketChannel_write_ByteBufferII.

/**
     * @tests SocketChannel#write(ByteBuffer[], int, int)
     */
public void test_socketChannel_write_ByteBufferII() throws Exception {
    // regression 2 for HARMONY-549
    ServerSocketChannel ssc = ServerSocketChannel.open();
    ssc.socket().bind(localAddr2);
    SocketChannel sc = SocketChannel.open();
    sc.connect(localAddr2);
    SocketChannel sock = ssc.accept();
    ByteBuffer[] buf = { ByteBuffer.allocate(10), null };
    try {
        sc.write(buf, 0, 2);
        fail("should throw NPE");
    } catch (NullPointerException e) {
    // expected
    }
    ssc.close();
    sc.close();
    ByteBuffer target = ByteBuffer.allocate(10);
    assertEquals(-1, sock.read(target));
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) ByteBuffer(java.nio.ByteBuffer) ServerSocketChannel(java.nio.channels.ServerSocketChannel)

Example 22 with ServerSocketChannel

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

the class SinkChannelTest method test_socketChannel_read_write.

public void test_socketChannel_read_write() throws Exception {
    ServerSocketChannel ssc = ServerSocketChannel.open();
    ssc.socket().bind(new InetSocketAddress(InetAddress.getLocalHost(), 49999));
    SocketChannel sc = SocketChannel.open();
    sc.connect(new InetSocketAddress(InetAddress.getLocalHost(), 49999));
    SocketChannel sock = ssc.accept();
    ByteBuffer[] buf = { ByteBuffer.allocate(10), null };
    try {
        sc.write(buf, 0, 2);
        fail("should throw NPE");
    } catch (NullPointerException expected) {
    }
    ssc.close();
    sc.close();
    ByteBuffer target = ByteBuffer.allocate(10);
    assertEquals(-1, sock.read(target));
}
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 23 with ServerSocketChannel

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

the class SocketChannelTest method test_write$NonBlockingException.

/**
     * @tests java.nio.channels.SocketChannel#write(ByteBuffer[])
     *
     * In non-blocking mode, the native system call will return EAGAIN/EWOULDBLOCK error
     * code on Linux/Unix and return WSATRY_AGAIN/WSAEWOULDBLOCK error code on Windows.
     * These error code means try again but not fatal error, so we should not throw exception.
     */
public void test_write$NonBlockingException() throws Exception {
    ServerSocketChannel ssc = ServerSocketChannel.open();
    ssc.configureBlocking(false);
    ssc.socket().bind(null);
    SocketChannel sc = SocketChannel.open();
    sc.configureBlocking(false);
    boolean connected = sc.connect(ssc.socket().getLocalSocketAddress());
    SocketChannel sock = ssc.accept();
    if (!connected) {
        sc.finishConnect();
    }
    try {
        for (int i = 0; i < 100; i++) {
            ByteBuffer buf1 = ByteBuffer.allocate(10);
            sc.socket().setSendBufferSize(512);
            int bufSize = sc.socket().getSendBufferSize();
            ByteBuffer buf2 = ByteBuffer.allocate(bufSize * 10);
            ByteBuffer[] sent = new ByteBuffer[2];
            sent[0] = buf1;
            sent[1] = buf2;
            sc.write(sent);
        }
    } finally {
        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 24 with ServerSocketChannel

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

the class SocketChannelTest method test_socketChannel_read_ByteBufferII_bufNULL.

/**
     * @tests SocketChannel#read(ByteBuffer[], int, int) with a null ByteBuffer
     */
public void test_socketChannel_read_ByteBufferII_bufNULL() throws Exception {
    // regression 3 for HARMONY-549
    ServerSocketChannel ssc = ServerSocketChannel.open();
    ssc.socket().bind(localAddr2);
    SocketChannel sc = SocketChannel.open();
    sc.connect(localAddr2);
    ssc.accept();
    ByteBuffer[] buf = new ByteBuffer[2];
    buf[0] = ByteBuffer.allocate(1);
    // let buf[1] be null
    try {
        sc.read(buf, 0, 2);
        fail("should throw NullPointerException");
    } catch (NullPointerException e) {
    // expected
    }
    ssc.close();
    sc.close();
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) ByteBuffer(java.nio.ByteBuffer) ServerSocketChannel(java.nio.channels.ServerSocketChannel)

Example 25 with ServerSocketChannel

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

the class SocketChannelTest method test_writev2.

/**
     * @tests java.nio.channels.SocketChannel#write(ByteBuffer[])
     */
public void test_writev2() throws Exception {
    ServerSocketChannel ssc = ServerSocketChannel.open();
    ssc.configureBlocking(false);
    ssc.socket().bind(null);
    SocketChannel sc = SocketChannel.open();
    sc.configureBlocking(false);
    boolean connected = sc.connect(ssc.socket().getLocalSocketAddress());
    SocketChannel sock = ssc.accept();
    if (!connected) {
        sc.finishConnect();
    }
    ByteBuffer buf1 = ByteBuffer.allocate(10);
    sc.socket().setSendBufferSize(512);
    int bufSize = sc.socket().getSendBufferSize();
    ByteBuffer buf2 = ByteBuffer.allocate(bufSize * 10);
    ByteBuffer[] sent = new ByteBuffer[2];
    sent[0] = buf1;
    sent[1] = buf2;
    long whole = buf1.remaining() + buf2.remaining();
    long write = sc.write(sent);
    ssc.close();
    sc.close();
    sock.close();
    assertTrue(whole == (write + buf1.remaining() + buf2.remaining()));
}
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)274 SocketChannel (java.nio.channels.SocketChannel)156 InetSocketAddress (java.net.InetSocketAddress)128 IOException (java.io.IOException)96 ByteBuffer (java.nio.ByteBuffer)53 SelectionKey (java.nio.channels.SelectionKey)41 ServerSocket (java.net.ServerSocket)37 Test (org.junit.Test)27 Socket (java.net.Socket)24 ClosedChannelException (java.nio.channels.ClosedChannelException)22 Selector (java.nio.channels.Selector)22 ClosedSelectorException (java.nio.channels.ClosedSelectorException)15 InetAddress (java.net.InetAddress)14 CancelledKeyException (java.nio.channels.CancelledKeyException)11 EOFException (java.io.EOFException)8 SocketException (java.net.SocketException)8 BindException (java.net.BindException)7 SocketAddress (java.net.SocketAddress)7 Test (org.junit.jupiter.api.Test)7 OutgoingPacket (com.twitter.heron.common.network.OutgoingPacket)6