java通过NIO实现时间服务器

NIO客户端以及服务端通信流程图
java通过NIO实现时间服务器_第1张图片
解读流程图
1、客户端和服务端通信是通过SocketChannel(包含ByteBuffer),且客户端和服务分别一个SocketChannel对象。
2、客户端与服务端都是从ByteBuffer读取/写入数据。

NIO开发时间服务器大致流程
服务端
1、新开一个线程,打开ServerSocketChannel并设置为非阻塞模式,监听一个端口,新建一个多路复用器Selector,将ServerSocketChannel注册到Selector并监听客户端的连接请求。
2、循环selector,将注册到上面的SelectionKey遍历出来, 如果是连接事件,将客户端SocketChannel注册到Selector,并设置为非阻塞,并监听可读状态。 如果是可读事件将用SocketChannel.read(ByteBuffer)将socketChannel里的数据读入到ByteBuffer里。再将ByteBuffer里的数据写入到字节数组。这样在向客户端写入数据时将字节数组的数据通过SocketChannel写入到ByteBuffer,从而完成写入客户端。

客户端:
1、通过SocketChannel.open()打开SocketChannel,通过Selector.open()打开多路复用器Selector,设置SocketChannel为非阻塞。
2、连接服务端boolean connect = socketChannel.connect(new InetSocketAddress(host, port));如果连接成功则将 socketChannel.register(selector, SelectionKey.OP_READ);socketChannel的可读事件注册到多路复用器。如果没有连接成功 socketChannel.register(selector, SelectionKey.OP_CONNECT);socketChannel的连接事件注册到多路复用器。
3、轮询多路复用器上的事件SocketChannel client = (SocketChannel) selectionKey.channel();
获取客户端SocketChannel,如果连接成功则SocketChannel注册到Selector,注册SelectionKey.OP_READ操作位。这时再向服务端写入数据(将数据通过SocketChannel写入到ByteBuffer)。
4、在通过轮询时判断selectionKey.isReadable()代表客户端收到服务器端的应答消息,则将服务端发送的数据写入到ByteBuffer,再将ByteBuffer里的数据写入到字节数组。

NIO优点
1、 客户端发起的连接操作是异步的,可以通过在多路复用器注册OP_CONNECT等待后续结果,不需要像之前的客户端那样被同步阻塞;
    2、 SocketChannel的读写操作都是异步的,如果没有可读写的数据它不会同步等待,直接返回,这样IO通信线程就可以处理其他的链路,不需要同步等待这个链路可用;
    3、 线程模型的优化:由于JDK的Selector在Linux等主流操作系统上通过 epoll实现,它没有连接句柄数的限制(只受限于操作系统的最大句柄数或者对单个进程的句柄限制),这意味着一个Selector线程可以同时处理成千上万个客户端连接,而且性能不会随着客户端的增加而线性下降。

NIO缺点
1、NIO类库和API复杂,使用麻烦。
  2、需要具备Java多线程编程能力(涉及到Reactor模式)。
 3、客户端断线重连、网络不稳定、半包读写、失败缓存、网络阻塞和异常码流等问题处理难度非常大
  4、存在部分BUG

服务端代码
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Date;
import java.util.Iterator;
import java.util.Set;

//单个线程处理请求
public class MultiplexerTimeServer implements Runnable {
	private Selector selector;
	private ServerSocketChannel serverChannel;
	private volatile boolean stop;//注意这个变量volatile修饰
	
	/**
	 * 构造函数
	 * @param port
	 */
	public MultiplexerTimeServer(int port) {
		try {
			//打开ServerSocketChannel
			serverChannel = ServerSocketChannel.open();
			//设置为非阻塞模式
			serverChannel.configureBlocking(false);
			//绑定监听的端口地址
			serverChannel.socket().bind(new InetSocketAddress(port), 1024);
			//创建Selector线程
			selector = Selector.open();
			//将ServerSocketChannel注册到Selector,交给Selector监听
			serverChannel.register(selector, SelectionKey.OP_ACCEPT);
			System.out.println("The time server is start in port:"+port);
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(1);
		}
	}

	public void stop(){
		this.stop = true;
	}
	@Override
	public void run() {
		while(!stop){
			try {
				//通过Selector循环准备就绪的Channel===>Key
				selector.select();
				Set selectionKeys = selector.selectedKeys();
				Iterator iterator = selectionKeys.iterator();
				SelectionKey selectionKey = null;
				while(iterator.hasNext()){
					selectionKey = iterator.next();
					iterator.remove();
					try {
						handleInput(selectionKey);
					} catch (Exception e) {
						if(selectionKey!=null){
							selectionKey.cancel();
							if(selectionKey.channel()!=null){
								selectionKey.channel().close();
							}
						}
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if(selector !=null){
			try {
				selector.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	private void handleInput(SelectionKey selectionKey) throws IOException {
		//判断key是否有效
		if(selectionKey.isValid()){
			if (selectionKey.isAcceptable()) {
				ServerSocketChannel server = (ServerSocketChannel) selectionKey.channel();
				//多路复用器监听到新的客户端连接,处理连接请求,完成TCP三次握手。
    //这个SocketChannel是和客户端进行通信的
				SocketChannel client = server.accept();
				//设置为非阻塞模式
				client.configureBlocking(false);
	            // 将新连接注册到多路复用器上,监听其读操作,读取客户端发送的消息。
	            client.register(selector, SelectionKey.OP_READ);
			}
			if(selectionKey.isReadable()){
				SocketChannel client = (SocketChannel) selectionKey.channel();
				ByteBuffer receivebuffer = ByteBuffer.allocate(1024);
				//读取客户端请求消息到缓冲区
				int count = client.read(receivebuffer);   //非阻塞    
				if (count > 0) {
					receivebuffer.flip();
					//remaining()方法返回Buffer中剩余的可读数据长度
					byte[] bytes = new byte[receivebuffer.remaining()]; 
					//从缓冲区读取消息
					receivebuffer.get(bytes);
					String body = new String(bytes, "UTF-8");
					System.out.println("The time server(Thread:"+Thread.currentThread()+") receive order : "+body);
					//将currentTime响应给客户端(客户端Channel)
					String currentTime = "QUERY TIME ORDER".equalsIgnoreCase(body) ? new Date(System.currentTimeMillis()).toString() : "BAD ORDER";
					doWrite(client, currentTime);
				}else if(count < 0){
					selectionKey.channel();
					client.close();
				}else{
					
				}
			}
		}
	}
	private void doWrite(SocketChannel client, String currentTime) throws IOException {
		if(currentTime != null && currentTime.trim().length()>0){
			ByteBuffer sendbuffer = ByteBuffer.allocate(1024);
			sendbuffer.put(currentTime.getBytes());
	        sendbuffer.flip();
	        //将客户端响应消息写入到客户端Channel中。
	        client.write(sendbuffer);
	        System.out.println("服务器端向客户端发送数据--:" + currentTime);
		}else{
			System.out.println("没有数据");
		}
	}
}
客户端代码
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;
public class TimeClientHandler implements Runnable {
	
	private String host;
	private int port;
	private SocketChannel socketChannel;
	private Selector selector;
	private volatile boolean stop;
	
	public TimeClientHandler(String host, int port) {
		this.host = host;
		this.port = port;
		try {
			//打开SocketChannel
			socketChannel = SocketChannel.open();
			//创建Selector线程
			selector = Selector.open();
			//设置为非阻塞模式
			socketChannel.configureBlocking(false);
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(1);
		}
	}

	@Override
	public void run() {
		try {
			doConnect();
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(1);
		}
		while(!stop){
			//轮训通道的状态
			try {
				selector.select(1000);
				Set selectionKeys = selector.selectedKeys();
				Iterator iterator = selectionKeys.iterator();
				SelectionKey selectionKey = null;
				while(iterator.hasNext()){
					selectionKey = iterator.next();
					iterator.remove();
					try {
						handleInput(selectionKey);
					} catch (Exception e) {
						if(selectionKey!=null){
							selectionKey.cancel();
							if(selectionKey.channel()!=null){
								selectionKey.channel().close();
							}
						}
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
				System.exit(1);
			}
		}
		if(selector !=null){
			try {
				selector.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	private void handleInput(SelectionKey selectionKey) throws Exception {
		if(selectionKey.isValid()){
			SocketChannel client = (SocketChannel) selectionKey.channel();
			if (selectionKey.isConnectable()){
				if(client.finishConnect()){
					client.register(selector, SelectionKey.OP_READ);
					doWrite(client);
				}else{
					System.exit(1);
				}
			}
			if (selectionKey.isReadable()) {
				ByteBuffer receivebuffer = ByteBuffer.allocate(1024);
				int count = client.read(receivebuffer);
				if (count > 0) {
					receivebuffer.flip();
					byte[] bytes = new byte[receivebuffer.remaining()]; //remaining()方法
					receivebuffer.get(bytes);
					String body = new String(bytes, "UTF-8");
					System.out.println("Now is "+body);
					this.stop = true;
				}else if(count < 0){
					selectionKey.channel();
					client.close();
				}else{
					
				}
			}
		}
	}

	private void doConnect() throws Exception {
		//连接服务端
		boolean connect = socketChannel.connect(new InetSocketAddress(host, port));
		//判断是否连接成功,如果连接成功,则监听Channel的读状态。
		if(connect){
			socketChannel.register(selector, SelectionKey.OP_READ);
			//写数据  写给服务端
			doWrite(socketChannel);
		}else{
			//如果没有连接成功,则向多路复用器注册Connect状态
			socketChannel.register(selector, SelectionKey.OP_CONNECT);
		}
		
	}
	private void doWrite(SocketChannel channel) throws IOException {
		ByteBuffer sendbuffer = ByteBuffer.allocate(1024);
		sendbuffer.put("QUERY TIME ORDER".getBytes());
        sendbuffer.flip();
        //向Channel中写入客户端的请求指令  写到服务端
        channel.write(sendbuffer);
        if(!sendbuffer.hasRemaining()){
        	System.out.println("Send order to server succeed.");
        }
	}
}



你可能感兴趣的:(NIO)