ServerSocketChannel的read阻塞

查看ifeve的教程,写了一个测试用例,用浏览器测试时,发现无法停止SocketChannel的read方法。

然后将SocketChannel设置为非阻塞进行了解决。

ServerSocketChannel ssc = ServerSocketChannel.open();
		ssc.bind(new InetSocketAddress(9999));
		ByteBuffer bb = ByteBuffer.allocate(10);
		SocketChannel sc = null;
		while(true){
			sc = ssc.accept();
			sc.configureBlocking(false);
			while(!sc.finishConnect()){
				
			}
			bb.clear();
			int c = sc.read(bb);
			while(c > 0){
				bb.flip();
				while(bb.hasRemaining()){
					System.out.print((char)bb.get());
				}
				bb.clear();
				c = sc.read(bb);
			}
			sc.close();
		}


你可能感兴趣的:(Java)