Example usage for java.nio.channels AsynchronousSocketChannel isOpen

List of usage examples for java.nio.channels AsynchronousSocketChannel isOpen

Introduction

In this page you can find the example usage for java.nio.channels AsynchronousSocketChannel isOpen.

Prototype

public boolean isOpen();

Source Link

Document

Tells whether or not this channel is open.

Usage

From source file:Test.java

private static void serverStart() {
    try {/*from  ww  w.  j  ava  2  s.  co m*/
        InetSocketAddress hostAddress = new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 2583);
        AsynchronousServerSocketChannel serverSocketChannel = AsynchronousServerSocketChannel.open()
                .bind(hostAddress);
        Future<AsynchronousSocketChannel> serverFuture = serverSocketChannel.accept();
        final AsynchronousSocketChannel clientSocket = serverFuture.get();
        if ((clientSocket != null) && (clientSocket.isOpen())) {
            InputStream connectionInputStream = Channels.newInputStream(clientSocket);
            ObjectInputStream ois = null;
            ois = new ObjectInputStream(connectionInputStream);
            while (true) {
                Object object = ois.readObject();
                if (object.equals("EOF")) {
                    clientSocket.close();
                    break;
                }
                System.out.println("Received :" + object);
            }
            ois.close();
            connectionInputStream.close();
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}