public class NioNonBlockingEchoServer { public static void main(String[] args) throws IOException { ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); serverSocketChannel.configureBlocking(false); serverSocketChannel.bind(new InetSocketAddress(7000)); while (active) { SocketChannel socketChannel = serverSocketChannel.accept(); // non-blocking if (socketChannel != null) { socketChannel.configureBlocking(false); ByteBuffer buffer = ByteBuffer.allocate(1024); while (true) { buffer.clear(); int read = socketChannel.read(buffer); // non-blocking if (read < 0) { break; } buffer.flip(); socketChannel.write(buffer); // can be non-blocking } socketChannel.close(); } } serverSocketChannel.close(); } }