论坛首页 Java企业应用论坛

很诡异的nio非阻塞通信

浏览 1659 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2012-01-04  
我先把代码贴出来吧

public class AServer {
	public static void main(String[] args) throws Exception {
		ServerSocketChannel server = ServerSocketChannel.open();
		server.configureBlocking(false);
		server.socket().bind(new InetSocketAddress(8080));
		Selector selector = Selector.open();
		server.register(selector, SelectionKey.OP_ACCEPT);
		
		while (true) {
			selector.select();
			System.out.println("selector.select()---------" + selector.select());
			//readKeys集合
			Iterator<SelectionKey> iter = selector.selectedKeys().iterator();
			while (iter.hasNext()) {
				SelectionKey key = iter.next();
				iter.remove();
				
				if (key.isAcceptable()) {
					System.out.println("acceptable");
//					ServerSocketChannel ssc = (ServerSocketChannel) key.channel();
					SocketChannel client = server.accept();
					client.configureBlocking(false);
					client.register(selector, SelectionKey.OP_READ);
				} else if (key.isReadable()) {
					System.out.println("this server is readable");
				}
			}
		}
	}
}


-------------------------------------------------------------------------------------------------------
public class AClient {
	public static void main(String[] args) throws Exception {
		SocketChannel client = SocketChannel.open();
		client.configureBlocking(false);
		Selector selector = Selector.open();
		client.register(selector, SelectionKey.OP_CONNECT);
		client.connect(new InetSocketAddress("localhost", 8080));
		
		while (true) {
			selector.select();
			//readKeys
			Iterator<SelectionKey> iter = selector.selectedKeys().iterator();
			while (iter.hasNext()) {
				SelectionKey key = iter.next();
				iter.remove();
				
				if (key.isConnectable()) {
//					System.out.println("connnect");
					SocketChannel socket = (SocketChannel) key.channel();
					socket.configureBlocking(false);
//					socket.register(selector, SelectionKey.OP_READ);
//					System.out.println("client is connected");
				} else if (key.isReadable()) {
					System.out.println("client is read");
				}
			}
		}
	}
}


-------------------------------------------------------------------------------------------------------
我注释掉AServer中的System.out.println("acceptable")和AClient中的System.out.println("connnect"),在AServer中就不能打印System.out.println("this server is readable")这句了。

还有两个问题就是我没有注掉System.out.println("acceptable")和System.out.println("connnect")时,AServer中一直会执行System.out.println("this server is readable");AClient也会一直执行System.out.println("connnect"),我感到疑惑的是执行了
SelectionKey key = iter.next();
				iter.remove();
这个之后,key不是被删除了么,不应该出现很多次
if (key.isConnectable()) {
//					System.out.println("connnect");
					SocketChannel socket = (SocketChannel) key.channel();
					socket.configureBlocking(false);
//					socket.register(selector, SelectionKey.OP_READ);
//					System.out.println("client is connected");
				} 
这里面打印的内容呀

还有个疑惑就是在AServer中
SocketChannel client = server.accept();
					client.configureBlocking(false);
					client.register(selector, SelectionKey.OP_READ);
执行了这段代码,client注册SelectionKey.OP_READ为可读的操作,是服务器端和客户端的key都是Readable可读的?如果是可读的为什么在AClient中没有打印
System.out.println("client is read");
这句呀。

由于才接触到nio这块,不是很熟悉,大家可以帮我看看么,感谢ing
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics