Example usage for java.nio.channels AsynchronousServerSocketChannel open

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

Introduction

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

Prototype

public static AsynchronousServerSocketChannel open() throws IOException 

Source Link

Document

Opens an asynchronous server-socket channel.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    AsynchronousServerSocketChannel server = AsynchronousServerSocketChannel.open();
    String host = "localhost";
    int port = 8989;
    InetSocketAddress sAddr = new InetSocketAddress(host, port);
    server.bind(sAddr);/*w w  w.  j  a  v a 2  s  . c o m*/
    System.out.format("Server is listening at %s%n", sAddr);
    Attachment attach = new Attachment();
    attach.server = server;
    server.accept(attach, new ConnectionHandler());
    Thread.currentThread().join();
}

From source file:Test.java

private static void serverStart() {
    try {/* w ww . j a va  2s . com*/
        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 {//www.  j a  va  2  s .com
            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();
}