`

io-nio-socket步步为营(三)NIO

 
阅读更多

原理:

运用reactor模式

Selector是核心-分发器A multiplexor of SelectableChannel objects。

能检测任意个注册过的channel上的事件,并分发事件,内部实现不用考虑,封装的好处

 

client没必要用NIO,使用http://luckywnj.iteye.com/blog/1744283中的client

server,

需要多线程的么?workthread?如何写?

 

/**
 * @author timeriver.wang
 * @version 2013-1-7 11:50:33 PM
 */
public class NIOServer {
	public static void main(String[] args) throws Exception {
		ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
		serverSocketChannel.socket().bind(new InetSocketAddress(8899));
		serverSocketChannel.configureBlocking(false);
		Selector selector = Selector.open();
		serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
		
		Charset charset = Charset.forName("UTF-8");
		CharsetDecoder decoder = charset.newDecoder();
		CharsetEncoder encoder = charset.newEncoder();
		ByteBuffer clientBuffer = ByteBuffer.allocate(1024);
		
		while (true) {
			// 貌似两个client同时连,返回也是1?在select处阻塞,这样避免线程卡死耗尽资源?
//		    int sel = selector.select();
			int sel = selector.select(3000);
			if (sel == 0) {
				continue;
			}
			Set<SelectionKey> set = selector.selectedKeys();
			System.out.println(set.size());
			Iterator<SelectionKey> keyIter = set.iterator();
			while (keyIter.hasNext()) {
				SelectionKey key = keyIter.next();
				System.out.println(key);
				// channel is ready to accept a new socket connection
				if (key.isAcceptable()) {
					SocketChannel serverChannel = ((ServerSocketChannel) key.channel()).accept();
//					为serverChannel再设置一次否则,java.nio.channels.IllegalBlockingModeException,
					serverChannel.configureBlocking(false);
					SelectionKey skey = serverChannel.register(key.selector(),SelectionKey.OP_READ);
					System.out.println("acceptable: serverChannel="+serverChannel+",skey="+skey);
				}else if (key.isReadable()) {// channel is ready for reading
					SocketChannel serverChannel = (SocketChannel) key.channel();
					clientBuffer.clear();
					long bytesRead = serverChannel.read(clientBuffer);
					if (bytesRead == -1) {
						serverChannel.close();
					} else {
						clientBuffer.flip();
						String receivedMsg = decoder.decode(clientBuffer).toString();
						System.out.println("receive msg: " + receivedMsg);
						SelectionKey skey = serverChannel.register(selector,SelectionKey.OP_WRITE);
						System.out.println("readable "+skey);
						skey.attach(receivedMsg);
					}
				} else if (key.isWritable()) { //channel is ready for writing
					SocketChannel serverChannel = (SocketChannel) key.channel();
					String sendMsg = (String) key.attachment();
					ByteBuffer block = encoder.encode(CharBuffer.wrap("Hello !"+sendMsg));
					serverChannel.write(block);
					SelectionKey skey = serverChannel.register(key.selector(),SelectionKey.OP_READ);
					System.out.println("write "+skey);
				}
				keyIter.remove();
			}
		}
	}
}
分享到:
评论

相关推荐

    xnio-nio-3.8.0.Final-API文档-中文版.zip

    赠送jar包:xnio-nio-3.8.0.Final.jar; 赠送原API文档:xnio-nio-3.8.0.Final-javadoc.jar; 赠送源代码:xnio-nio-3.8.0.Final-sources.jar; 赠送Maven依赖信息文件:xnio-nio-3.8.0.Final.pom; 包含翻译后的API...

    httpcore-nio-4.4.15-API文档-中文版.zip

    赠送jar包:httpcore-nio-4.4.15.jar 赠送原API文档:httpcore-nio-4.4.15-javadoc.jar 赠送源代码:httpcore-nio-4.4.15-sources.jar 包含翻译后的API文档:httpcore-nio-4.4.15-javadoc-API文档-中文(简体)版....

    httpcore-nio-4.4.10-API文档-中英对照版.zip

    赠送jar包:httpcore-nio-4.4.10.jar; 赠送原API文档:httpcore-nio-4.4.10-javadoc.jar; 赠送源代码:httpcore-nio-4.4.10-sources.jar; 赠送Maven依赖信息文件:httpcore-nio-4.4.10.pom; 包含翻译后的API文档...

    xnio-nio-3.8.4.Final-API文档-中英对照版.zip

    赠送jar包:xnio-nio-3.8.4.Final.jar; 赠送原API文档:xnio-nio-3.8.4.Final-javadoc.jar; 赠送源代码:xnio-nio-3.8.4.Final-sources.jar; 赠送Maven依赖信息文件:xnio-nio-3.8.4.Final.pom; 包含翻译后的API...

    httpcore-nio-4.4.6-API文档-中文版.zip

    赠送jar包:httpcore-nio-4.4.6.jar 赠送原API文档:httpcore-nio-4.4.6-javadoc.jar 赠送源代码:httpcore-nio-4.4.6-sources.jar 包含翻译后的API文档:httpcore-nio-4.4.6-javadoc-API文档-中文(简体)版.zip ...

    httpcore-nio-4.4.15-API文档-中英对照版.zip

    赠送jar包:httpcore-nio-4.4.15.jar 赠送原API文档:httpcore-nio-4.4.15-javadoc.jar 赠送源代码:httpcore-nio-4.4.15-sources.jar 包含翻译后的API文档:httpcore-nio-4.4.15-javadoc-API文档-中文(简体)-...

    httpcore-nio-4.3.jar包

    《深入解析httpcore-nio-4.3.jar:构建高性能的Java非阻塞网络通信》 在Java网络编程中,高效、稳定且可扩展的通信框架至关重要。Apache HttpComponents项目中的HttpCore NIO模块(httpcore-nio)就是这样一个框架...

    httpcore-nio-4.4.10-API文档-中文版.zip

    赠送jar包:httpcore-nio-4.4.10.jar; 赠送原API文档:httpcore-nio-4.4.10-javadoc.jar; 赠送源代码:httpcore-nio-4.4.10-sources.jar; 赠送Maven依赖信息文件:httpcore-nio-4.4.10.pom; 包含翻译后的API文档...

    httpcore-nio-4.4.5-API文档-中文版.zip

    赠送jar包:httpcore-nio-4.4.5.jar; 赠送原API文档:httpcore-nio-4.4.5-javadoc.jar; 赠送源代码:httpcore-nio-4.4.5-sources.jar; 赠送Maven依赖信息文件:httpcore-nio-4.4.5.pom; 包含翻译后的API文档:...

    JAVA IO-NIO 详解

    #### 三、NIO核心组件详解 NIO的核心组件主要包括Channel(通道)、Buffer(缓冲区)和Selector(选择器)。 **1. Channel** - **概念**: 是数据传输的通道,类似于传统IO中的流,但功能更强大。 - **分类**: ...

    xnio-nio-3.8.4.Final-API文档-中文版.zip

    赠送jar包:xnio-nio-3.8.4.Final.jar; 赠送原API文档:xnio-nio-3.8.4.Final-javadoc.jar; 赠送源代码:xnio-nio-3.8.4.Final-sources.jar; 赠送Maven依赖信息文件:xnio-nio-3.8.4.Final.pom; 包含翻译后的API...

    xnio-nio-3.8.0.Final-API文档-中英对照版.zip

    赠送jar包:xnio-nio-3.8.0.Final.jar; 赠送原API文档:xnio-nio-3.8.0.Final-javadoc.jar; 赠送源代码:xnio-nio-3.8.0.Final-sources.jar; 赠送Maven依赖信息文件:xnio-nio-3.8.0.Final.pom; 包含翻译后的API...

    httpcore-nio-4.4.6-API文档-中英对照版.zip

    赠送jar包:httpcore-nio-4.4.6.jar; 赠送原API文档:httpcore-nio-4.4.6-javadoc.jar; 赠送源代码:httpcore-nio-4.4.6-sources.jar; 赠送Maven依赖信息文件:httpcore-nio-4.4.6.pom; 包含翻译后的API文档:...

    httpcore-nio-4.4.14-API文档-中文版.zip

    赠送jar包:httpcore-nio-4.4.14.jar; 赠送原API文档:httpcore-nio-4.4.14-javadoc.jar; 赠送源代码:httpcore-nio-4.4.14-sources.jar; 赠送Maven依赖信息文件:httpcore-nio-4.4.14.pom; 包含翻译后的API文档...

    基于java的开发源码-NIO网络框架 xSocket.zip

    基于java的开发源码-NIO网络框架 xSocket.zip 基于java的开发源码-NIO网络框架 xSocket.zip 基于java的开发源码-NIO网络框架 xSocket.zip 基于java的开发源码-NIO网络框架 xSocket.zip 基于java的开发源码-NIO网络...

    httpcore-nio-4.4.12-API文档-中文版.zip

    赠送jar包:httpcore-nio-4.4.12.jar; 赠送原API文档:httpcore-nio-4.4.12-javadoc.jar; 赠送源代码:httpcore-nio-4.4.12-sources.jar; 赠送Maven依赖信息文件:httpcore-nio-4.4.12.pom; 包含翻译后的API文档...

    httpcore-nio-4.4.9.jar

    httpcore-nio-4.4.9.jar

    httpcore-nio-4.4.12.jar

    java运行依赖jar包

    android-socket-nio-master.zip

    "android-socket-nio-master.zip" 是一个关于Android中使用Socket结合NIO实现高效通信的项目,其目标是提高Socket通信的性能和处理大量并发连接的能力。 NIO(非阻塞I/O)是Java提供的一个替代传统I/O的API,主要...

    httpcore-nio-4.4.4-API文档-中文版.zip

    赠送jar包:httpcore-nio-4.4.4.jar; 赠送原API文档:httpcore-nio-4.4.4-javadoc.jar; 赠送源代码:httpcore-nio-4.4.4-sources.jar; 赠送Maven依赖信息文件:httpcore-nio-4.4.4.pom; 包含翻译后的API文档:...

Global site tag (gtag.js) - Google Analytics