zoukankan      html  css  js  c++  java
  • Nio学习4——EchoServer在IO,NIO,NIO.2中的实现

    堵塞IO实现:

    public class PlainEchoServer {
    	public void serve(int port) throws IOException {
    		final ServerSocket socket = new ServerSocket(port);
    		try {
    			while (true) {
    				final Socket clientSocket = socket.accept();
    				System.out.println("Accepted connection from " + clientSocket);
    				new Thread(new Runnable() {
    					@Override
    					public void run() {
    						try {
    							BufferedReader reader = new BufferedReader(
    									new InputStreamReader(
    											clientSocket.getInputStream()));
    							PrintWriter writer = new PrintWriter(
    									clientSocket.getOutputStream(), true);
    							while (true) {
    								writer.println(reader.readLine());
    								writer.flush();
    							}
    						} catch (IOException e) {
    							e.printStackTrace();
    							try {
    								clientSocket.close();
    							} catch (IOException ex) {
    								// ignore on close
    							}
    						}
    					}
    				}).start();
    			}
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    }
    

    
    非堵塞IO(NIO)实现:
    

    public class PlainNioEchoServer {
    	public void serve(int port) throws IOException {
    		System.out.println("Listening for connections on port " + port);
    		ServerSocketChannel serverChannel = ServerSocketChannel.open();
    		ServerSocket ss = serverChannel.socket();
    		InetSocketAddress address = new InetSocketAddress(port);
    		ss.bind(address);
    		serverChannel.configureBlocking(false);
    		Selector selector = Selector.open();
    		serverChannel.register(selector, SelectionKey.OP_ACCEPT);
    		while (true) {
    			try {
    				selector.select();
    			} catch (IOException ex) {
    				ex.printStackTrace();
    				// handle in a proper way
    				break;
    			}
    			Set readyKeys = selector.selectedKeys();
    			Iterator iterator = readyKeys.iterator();
    			while (iterator.hasNext()) {
    				SelectionKey key = (SelectionKey) iterator.next();
    				iterator.remove();
    				try {
    					if (key.isAcceptable()) {
    						ServerSocketChannel server = (ServerSocketChannel) key
    								.channel();
    						SocketChannel client = server.accept();
    						System.out
    								.println("Accepted connection from " + client);
    						client.configureBlocking(false);
    						client.register(selector, SelectionKey.OP_WRITE
    								| SelectionKey.OP_READ,
    								ByteBuffer.allocate(100));
    					}
    					if (key.isReadable()) {
    						SocketChannel client = (SocketChannel) key.channel();
    						ByteBuffer output = (ByteBuffer) key.attachment();
    						client.read(output);
    					}
    					if (key.isWritable()) {
    						SocketChannel client = (SocketChannel) key.channel();
    						ByteBuffer output = (ByteBuffer) key.attachment();
    						output.flip();
    						client.write(output);
    						output.compact();
    					}
    				} catch (IOException ex) {
    					key.cancel();
    					try {
    						key.channel().close();
    					} catch (IOException cex) {
    					}
    				}
    			}
    		}
    	}
    }
    异步IO(NIO.2)实现:

    public class PlainNio2EchoServer {
    	public void serve(int port) throws IOException {
    		System.out.println("Listening for connections on port " + port);
    		final AsynchronousServerSocketChannel serverChannel = AsynchronousServerSocketChannel
    				.open();
    		InetSocketAddress address = new InetSocketAddress(port);
    		serverChannel.bind(address);
    		final CountDownLatch latch = new CountDownLatch(1);
    		serverChannel.accept(null,
    				new CompletionHandler<AsynchronousSocketChannel, Object>() {
    					@Override
    					public void completed(
    							final AsynchronousSocketChannel channel,
    							Object attachment) {
    						serverChannel.accept(null, this);
    						ByteBuffer buffer = ByteBuffer.allocate(100);
    						channel.read(buffer, buffer, new EchoCompletionHandler(
    								channel));
    					}
    
    					@Override
    					public void failed(Throwable throwable, Object attachment) {
    						try {
    							serverChannel.close();
    						} catch (IOException e) {
    							// ingnore on close
    						} finally {
    							latch.countDown();
    						}
    					}
    				});
    		try {
    			latch.await();
    		} catch (InterruptedException e) {
    			Thread.currentThread().interrupt();
    		}
    	}
    
    	private final class EchoCompletionHandler implements
    			CompletionHandler<Integer, ByteBuffer> {
    		private final AsynchronousSocketChannel channel;
    
    		EchoCompletionHandler(AsynchronousSocketChannel channel) {
    			this.channel = channel;
    		}
    
    		@Override
    		public void completed(Integer result, ByteBuffer buffer) {
    			buffer.flip();
    			channel.write(buffer, buffer,
    					new CompletionHandler<Integer, ByteBuffer>() {
    						@Override
    						public void completed(Integer result, ByteBuffer buffer) {
    							if (buffer.hasRemaining()) {
    								channel.write(buffer, buffer, this);
    							} else {
    								buffer.compact();
    								channel.read(buffer, buffer,
    										EchoCompletionHandler.this);
    							}
    						}
    
    						@Override
    						public void failed(Throwable exc, ByteBuffer attachment) {
    							try {
    								channel.close();
    							} catch (IOException e) {
    								// ingnore on close
    							}
    						}
    					});
    		}
    
    		@Override
    		public void failed(Throwable exc, ByteBuffer attachment) {
    			try {
    				channel.close();
    			} catch (IOException e) {
    				// ingnore on close
    			}
    		}
    	}
    }

    At first glance, it may look like more code than what you had when using the previous NIO
    implementation. But notice that NIO.2 handles threading and the creation of the so-called
    event loop for you. This approach simplifies the code needed to build a multithreaded NIO
    application, even though it may not seem to be the case in this example. As the complexity of
    the application increases, the gains become more evident because you’ll be producing cleaner
    code.


  • 相关阅读:
    Thymeleaf 异常:Exception processing template "index": An error happened during template parsing (template: "class path resource [templates/index.html]")
    Java 异常 Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date'
    MySQL 查询字段时,区分大小写
    Oracle Database XE 11gR2 SQL 命令行的显示调整
    使用 PL/SQL Developer 导入 .sql 文件
    下载、安装 PL/SQL Developer
    安装、验证安装 Oracle Database XE 11gR2
    下载 Oracle Database XE 11gR2
    MyEclipse 设置打开 jsp 文件的默认编辑器
    Oracle Database XE 11gR2 自带的用户,新建用户,修改用户密码
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/4371919.html
Copyright © 2011-2022 走看看