Example usage for java.nio.channels AsynchronousServerSocketChannel accept

List of usage examples for java.nio.channels AsynchronousServerSocketChannel accept

Introduction

In this page you can find the example usage for java.nio.channels AsynchronousServerSocketChannel accept.

Prototype

public abstract Future<AsynchronousSocketChannel> accept();

Source Link

Document

Accepts a connection.

Usage

From source file:Test.java

private static void serverStart() {
    try {//  ww  w  . j a v a  2  s  .c  om
        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();
    }
}

From source file:org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin.java

private void startConnection() throws IOException {
    Launcher<JavaLanguageClient> launcher;
    ExecutorService executorService = Executors.newCachedThreadPool();
    protocol = new JDTLanguageServer(projectsManager, preferenceManager);
    if (JDTEnvironmentUtils.inSocketStreamDebugMode()) {
        String host = JDTEnvironmentUtils.getClientHost();
        Integer port = JDTEnvironmentUtils.getClientPort();
        InetSocketAddress inetSocketAddress = new InetSocketAddress(host, port);
        AsynchronousServerSocketChannel serverSocket = AsynchronousServerSocketChannel.open()
                .bind(inetSocketAddress);
        try {/*from   w w  w.j  ava 2 s  . co  m*/
            AsynchronousSocketChannel socketChannel = serverSocket.accept().get();
            InputStream in = Channels.newInputStream(socketChannel);
            OutputStream out = Channels.newOutputStream(socketChannel);
            Function<MessageConsumer, MessageConsumer> messageConsumer = it -> it;
            launcher = Launcher.createIoLauncher(protocol, JavaLanguageClient.class, in, out, executorService,
                    messageConsumer);
        } catch (InterruptedException | ExecutionException e) {
            throw new RuntimeException("Error when opening a socket channel at " + host + ":" + port + ".", e);
        }
    } else {
        ConnectionStreamFactory connectionFactory = new ConnectionStreamFactory();
        InputStream in = connectionFactory.getInputStream();
        OutputStream out = connectionFactory.getOutputStream();
        Function<MessageConsumer, MessageConsumer> wrapper;
        if ("false".equals(System.getProperty("watchParentProcess"))) {
            wrapper = it -> it;
        } else {
            wrapper = new ParentProcessWatcher(this.languageServer);
        }
        launcher = Launcher.createLauncher(protocol, JavaLanguageClient.class, in, out, executorService,
                wrapper);
    }
    protocol.connectClient(launcher.getRemoteProxy());
    launcher.startListening();
}