java7 nio 例子
2014-07-02 09:52
99 查看
/** * */ package Test; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.Socket; 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; /** * * @author Administrator * @version */ public class NBTest { /** Creates new NBTest */ public NBTest() { } public void startServer() throws Exception { int channels = 0; int nKeys = 0; int currentSelector = 0; // 使用Selector Selector selector = Selector.open(); // 建立Channel 并绑定到9000端口 ServerSocketChannel ssc = ServerSocketChannel.open(); InetSocketAddress address = new InetSocketAddress( InetAddress.getLocalHost(), 9000); ssc.socket().bind(address); // 使设定non-blocking的方式。 ssc.configureBlocking(false); // 向Selector注册Channel及我们有兴趣的事件 SelectionKey s = ssc.register(selector, SelectionKey.OP_ACCEPT); printKeyInfo(s); while (true) // 不断的轮询 { debug("NBTest: Starting select"); // Selector通过select方法通知我们我们感兴趣的事件发生了。 nKeys = selector.select(); // 如果有我们注册的事情发生了,它的传回值就会大于0 if (nKeys > 0) { debug("NBTest: Number of keys after select operation: " + nKeys); // Selector传回一组SelectionKeys // 我们从这些key中的channel()方法中取得我们刚刚注册的channel。 Set selectedKeys = selector.selectedKeys(); Iterator i = selectedKeys.iterator(); while (i.hasNext()) { s = (SelectionKey) i.next(); printKeyInfo(s); debug("NBTest: Nr Keys in selector: " + selector.keys().size()); // 一个key被处理完成后,就都被从就绪关键字(ready keys)列表中除去 i.remove(); if (s.isAcceptable()) { // 从channel()中取得我们刚刚注册的channel。 Socket socket = ((ServerSocketChannel) s.channel()) .accept().socket(); SocketChannel sc = socket.getChannel(); sc.configureBlocking(false); sc.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE); System.out.println(++channels); } else { debug("NBTest: Channel not acceptable"); } } } else { debug("NBTest: Select finished without any keys."); } } } private static void debug(String s) { System.out.println(s); } private static void printKeyInfo(SelectionKey sk) { String s = new String(); s = "Att: " + (sk.attachment() == null ? "no" : "yes"); s += ", Read: " + sk.isReadable(); s += ", Acpt: " + sk.isAcceptable(); s += ", Cnct: " + sk.isConnectable(); s += ", Wrt: " + sk.isWritable(); s += ", Valid: " + sk.isValid(); s += ", Ops: " + sk.interestOps(); debug(s); } /** * @param args * the command line arguments */ public static void main(String args[]) { NBTest nbTest = new NBTest(); try { nbTest.startServer(); } catch (Exception e) { e.printStackTrace(); } } }
相关文章推荐
- NIO的简单例子
- Java NIO——6 基于非阻塞编程UDP NIO的例子
- NIO通信例子
- NIO一些例子
- NIO服务器和客户端通讯简单例子
- NIO与传统IO的区别 NIO Socket例子 实例
- BIO, NIO和 AIO的代码例子
- JAVA NIO 例子
- java.nio将一个文件的内容写入到另一个的文件简单例子
- java关于(io&nio) 的 文件copy例子
- nio udp server client Demo例子(五)
- 基于MINA构建简单高性能的NIO应用-一个简单的例子
- 关于NIO的理解及相关的例子
- BIO与NIO比较及例子
- 【Java】NIO 客户端-服务器 聊天 例子
- Java7中NIO学习之Path
- Netty学习笔记<2>--简单的NIO例子
- tcp nio 服务端、客户端例子--参考《分布式Java应用:基础与实践》
- 以NIO通信例子结合Jconsole解释JVM内存分配机制
- Java NIO——6 基于非阻塞编程UDP NIO的例子