NIO的工作方式(一)

package com.yyj.nio;

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.Iterator;
import java.util.Set;

public class NIODemo {
	
	public void selector() throws IOException{
		
		ByteBuffer buffer = ByteBuffer.allocate(1024); //定义一个缓冲区数组长度为1024
		//打开选择器
		Selector selector = Selector.open();
		ServerSocketChannel ssc = ServerSocketChannel.open();
		
		ssc.configureBlocking(false);//设置为非阻塞方式
		
		ssc.socket().bind(new InetSocketAddress(8080));
		ssc.register(selector, SelectionKey.OP_ACCEPT); //注册监听事件
		while(true){
			Set selectedKeys = selector.selectedKeys();//取得所有key集合
			Iterator it =  selectedKeys.iterator();
			while(it.hasNext()){
				SelectionKey key = (SelectionKey) it.next();
				
				if((key.readyOps() & SelectionKey.OP_ACCEPT) == SelectionKey.OP_ACCEPT){
					ServerSocketChannel ssChannel = (ServerSocketChannel) key.channel();
					SocketChannel sc = ssChannel.accept();
					sc.configureBlocking(false);
					sc.register(selector, SelectionKey.OP_READ);
					it.remove();
				}else if((key.readyOps() & SelectionKey.OP_READ)  == SelectionKey.OP_READ){
					SocketChannel sc = (SocketChannel) key.channel();
					while(true){
						buffer.clear();
						int n = sc.read(buffer);
						if(n<0){
							break;
						}
						
						buffer.flip();
					}
					it.remove();
				}
				
				
				
				
				
				
				
				
				
				
				
			}
		}
		
	}
	
	
	
}

你可能感兴趣的:(IO,线程,网络编程,NIO)