Java NIO2 -- AIO Callable -- Future

package com.xiuye.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.StandardSocketOptions;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;


public class NIO2_AIO_Server1 {

	private ExecutorService taskExecutor;
	private AsynchronousServerSocketChannel serverChannel;
	class Worker implements Callable<String>{

		private CharBuffer charBuffer;
		private CharsetDecoder decoder = Charset.defaultCharset().newDecoder();
		private AsynchronousSocketChannel channel;
		public Worker(AsynchronousSocketChannel channel) {
			this.channel = channel;
		}

		@Override
		public String call() throws Exception {

			final ByteBuffer buffer = ByteBuffer.allocate(1024);
			while(channel.read(buffer).get() != -1){
				buffer.flip();
				charBuffer = decoder.decode(buffer);
				String request = charBuffer.toString().trim();
				System.out.println("Client Request MSG: "+request);
				ByteBuffer outBuffer = ByteBuffer.wrap("Request Received!".getBytes());
				channel.write(outBuffer).get();
				if(buffer.hasRemaining()){
					buffer.compact();
				}
				else{
					buffer.clear();
				}
			}

			channel.close();
			return "OK";
		}

	}

	public void init() throws IOException{
		taskExecutor = Executors.newCachedThreadPool(Executors.defaultThreadFactory());
		serverChannel = AsynchronousServerSocketChannel.open();
		if(serverChannel.isOpen()){
			serverChannel.setOption(StandardSocketOptions.SO_RCVBUF, 4*1024);
			serverChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
			serverChannel.bind(new InetSocketAddress(8080));
		}
		else{
			throw new RuntimeException("Channel not opened!");
		}
	}

	public void start(){
		System.out.println("Wait for client ...");
		while(true){//虽然是异步还是有循环啊
			Future<AsynchronousSocketChannel> future = serverChannel.accept();

			AsynchronousSocketChannel channel;
			try {
				channel = future.get();
				taskExecutor.submit(new Worker(channel));
			} catch (InterruptedException | ExecutionException e) {
				e.printStackTrace();
				System.err.println("Server closed!");
				while(!taskExecutor.isTerminated()){}
				break;
			}



		}
	}

	public static void main(String[] args) throws IOException {

		NIO2_AIO_Server1 server = new NIO2_AIO_Server1();
		server.init();
		server.start();

	}

}


package com.xiuye.nio;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.StandardSocketOptions;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.util.concurrent.ExecutionException;

public class NIO2_AIO_Client1 {

	private AsynchronousSocketChannel channel;
	private CharBuffer charBuffer;
	private CharsetDecoder decoder = Charset.defaultCharset().newDecoder();
	private BufferedReader clientInput = new BufferedReader(new InputStreamReader(System.in));

	public void init() throws IOException, InterruptedException, ExecutionException{
		channel = AsynchronousSocketChannel.open();
		if(channel.isOpen()){
			channel.setOption(StandardSocketOptions.SO_RCVBUF, 128*1024);
			channel.setOption(StandardSocketOptions.SO_SNDBUF, 128*1024);
			channel.setOption(StandardSocketOptions.SO_KEEPALIVE, true);
			Void connect = channel.connect(new InetSocketAddress("127.0.0.1",8080)).get();
			if(connect != null){
				throw new RuntimeException("connect failed!");
			}

		}
		else{
			throw new RuntimeException("Channel not opened!");
		}
	}

	public void start() throws IOException, InterruptedException, ExecutionException{
		System.out.println("Input Client Resqust MSG:");
		String request = clientInput.readLine();
		channel.write(ByteBuffer.wrap(request.getBytes()));
		ByteBuffer buffer = ByteBuffer.allocate(1024);
		while(channel.read(buffer).get() != -1){
			buffer.flip();
			charBuffer = decoder.decode(buffer);
			String response = charBuffer.toString().trim();
			System.out.println("Server replied MSG: "+response);
			if(buffer.hasRemaining()){
				buffer.compact();
			}
			else{
				buffer.clear();
			}
			request = clientInput.readLine();
			channel.write(ByteBuffer.wrap(request.getBytes())).get();
		}
	}

	public static void main(String[] args) throws IOException, InterruptedException, ExecutionException {
		NIO2_AIO_Client1 client = new NIO2_AIO_Client1();
		client.init();
		client.start();
	}

}





阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页