List of usage examples for java.nio.channels AsynchronousSocketChannel close
@Override void close() throws IOException;
From source file:Test.java
private static void serverStart() { try {/* ww w . java2 s .c o 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(); } }
From source file:Test.java
private static void clientStart() { try {//from w w w . j a v a 2 s . c o m InetSocketAddress hostAddress = new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 2583); AsynchronousSocketChannel clientSocketChannel = AsynchronousSocketChannel.open(); Future<Void> connectFuture = clientSocketChannel.connect(hostAddress); connectFuture.get(); // Wait until connection is done. OutputStream os = Channels.newOutputStream(clientSocketChannel); ObjectOutputStream oos = new ObjectOutputStream(os); for (int i = 0; i < 5; i++) { oos.writeObject("Look at me " + i); Thread.sleep(1000); } oos.writeObject("EOF"); oos.close(); clientSocketChannel.close(); } catch (Exception e) { e.printStackTrace(); } }