Search in sources :

Example 26 with ServerSocketChannel

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

the class SocketChannelTest method test_finishConnect.

/**
     * Regression test for Harmony-1947.
     */
public void test_finishConnect() throws Exception {
    SocketAddress address = new InetSocketAddress("localhost", 0);
    ServerSocketChannel theServerChannel = ServerSocketChannel.open();
    ServerSocket serversocket = theServerChannel.socket();
    serversocket.setReuseAddress(true);
    // Bind the socket
    serversocket.bind(address);
    boolean doneNonBlockingConnect = false;
    // Loop so that we make sure we're definitely testing finishConnect()
    while (!doneNonBlockingConnect) {
        channel1 = SocketChannel.open();
        // Set the SocketChannel to non-blocking so that connect(..) does
        // not block
        channel1.configureBlocking(false);
        boolean connected = channel1.connect(new InetSocketAddress("localhost", serversocket.getLocalPort()));
        if (!connected) {
            // Now set the SocketChannel back to blocking so that
            // finishConnect() blocks.
            channel1.configureBlocking(true);
            doneNonBlockingConnect = channel1.finishConnect();
        }
        if (doneNonBlockingConnect) {
            tryFinish();
        }
        channel1.close();
    }
    if (!serversocket.isClosed()) {
        serversocket.close();
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ServerSocket(java.net.ServerSocket) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) ServerSocketChannel(java.nio.channels.ServerSocketChannel)

Example 27 with ServerSocketChannel

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

the class OldSocketChannelTest method test_socketChannel_read_DirectByteBuffer.

public void test_socketChannel_read_DirectByteBuffer() throws InterruptedException, IOException {
    // RoboVM note: This test has been modified to properly close down the ServerThread and
    // to close the SocketChannels opened even if the test fails half-way through.
    Thread server = null;
    try {
        final ServerSocketChannel ssc = ServerSocketChannel.open();
        ssc.socket().bind(null, 0);
        server = new Thread() {

            @Override
            public void run() {
                try {
                    for (int i = 0; i < 2; ++i) {
                        ByteBuffer buf = ByteBuffer.allocate(10);
                        buf.put(data);
                        buf.rewind();
                        ssc.accept().write(buf);
                    }
                } catch (Exception ignored) {
                }
            }
        };
        server.start();
        // First test with array based byte buffer
        ByteBuffer buf = null;
        SocketChannel sc = SocketChannel.open();
        try {
            sc.connect(ssc.socket().getLocalSocketAddress());
            buf = ByteBuffer.allocate(data.length);
            buf.limit(data.length / 2);
            sc.read(buf);
            buf.limit(buf.capacity());
            sc.read(buf);
        } finally {
            sc.close();
        }
        // Make sure the buffer is filled correctly
        buf.rewind();
        assertSameContent(data, buf);
        // Now test with direct byte buffer
        sc = SocketChannel.open();
        try {
            sc.connect(ssc.socket().getLocalSocketAddress());
            buf = ByteBuffer.allocateDirect(data.length);
            buf.limit(data.length / 2);
            sc.read(buf);
            buf.limit(buf.capacity());
            sc.read(buf);
        } finally {
            sc.close();
        }
        // Make sure the buffer is filled correctly
        buf.rewind();
        assertSameContent(data, buf);
    } finally {
        done = true;
        server.interrupt();
        server.join(2000);
    }
}
Also used : ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) ByteBuffer(java.nio.ByteBuffer) ServerSocketChannel(java.nio.channels.ServerSocketChannel) NoConnectionPendingException(java.nio.channels.NoConnectionPendingException) UnsupportedAddressTypeException(java.nio.channels.UnsupportedAddressTypeException) IOException(java.io.IOException) SocketException(java.net.SocketException) NotYetConnectedException(java.nio.channels.NotYetConnectedException) UnresolvedAddressException(java.nio.channels.UnresolvedAddressException)

Example 28 with ServerSocketChannel

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

the class SelectorTest method test_57456.

public void test_57456() throws Exception {
    Selector selector = Selector.open();
    ServerSocketChannel ssc = ServerSocketChannel.open();
    try {
        // Connect.
        ssc.configureBlocking(false);
        ssc.socket().bind(null);
        SocketChannel sc = SocketChannel.open();
        sc.connect(ssc.socket().getLocalSocketAddress());
        sc.finishConnect();
        // Switch to non-blocking so we can use a Selector.
        sc.configureBlocking(false);
        // Have the 'server' write something.
        ssc.accept().write(ByteBuffer.allocate(128));
        // At this point, the client should be able to read or write immediately.
        // (It shouldn't be able to connect because it's already connected.)
        SelectionKey key = sc.register(selector, SelectionKey.OP_CONNECT | SelectionKey.OP_READ | SelectionKey.OP_WRITE);
        assertEquals(1, selector.select());
        assertEquals(SelectionKey.OP_READ | SelectionKey.OP_WRITE, key.readyOps());
        assertEquals(0, selector.select());
    } 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 29 with ServerSocketChannel

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

the class OldServerSocketChannelTest method testConstructor.

// -------------------------------------------------------------------
// Test for methods in abstract class.
// -------------------------------------------------------------------
public void testConstructor() throws IOException {
    ServerSocketChannel channel = SelectorProvider.provider().openServerSocketChannel();
    assertNotNull(channel);
    assertSame(SelectorProvider.provider(), channel.provider());
}
Also used : ServerSocketChannel(java.nio.channels.ServerSocketChannel)

Example 30 with ServerSocketChannel

use of java.nio.channels.ServerSocketChannel in project java-in-action by xinghalo.

the class Server method main.

public static void main(String[] args) throws IOException {
    ThreadPoolExecutor excutor = new ThreadPoolExecutor(3, 10, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(100));
    ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
    serverSocketChannel.socket().bind(new InetSocketAddress(12345));
    while (true) {
        SocketChannel socketChannel = serverSocketChannel.accept();
        if (socketChannel != null) {
            System.out.println("接收心情求,启动新线程处理");
            excutor.submit(new SocketChannelThread(socketChannel));
        }
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) InetSocketAddress(java.net.InetSocketAddress) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) 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