java的selector用法
来源:互联网 发布:阿里云的cdn怎么样 编辑:程序博客网 时间:2024/04/30 14:46
对于java的NIO我不是很了解,正在学习中,以下是一个例子,可以读取telnet的输入
package com.test.nio;import java.io.DataInputStream;import java.io.InputStream;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.spi.SelectorProvider;import java.util.Iterator;import java.util.Set;public class TestSelector {/** * @param args * @throws Exception */public static void main(String[] args) throws Exception {// TODO Auto-generated method stubSelector selector=SelectorProvider.provider().openSelector();ServerSocketChannel ssc= ServerSocketChannel.open();ssc.configureBlocking(false);ssc.socket().bind(new InetSocketAddress("127.0.0.1",7979));SelectionKey acceptKey=ssc.register(selector,SelectionKey.OP_ACCEPT);//System.out.println("通道就绪个数:"+selector.select());int keysAdded=0;while((keysAdded=selector.select())>0){Set readyKeys=selector.selectedKeys();Iterator i=readyKeys.iterator();while(i.hasNext()){SelectionKey sk=(SelectionKey) i.next();i.remove();ServerSocketChannel nextReady= (ServerSocketChannel) sk.channel();Socket s=nextReady.accept().socket();InputStream din=s.getInputStream();DataInputStream ds=new DataInputStream(din);String str=ds.readLine();while(str!=null){System.out.println(str);str=ds.readLine();}}}}}
使用telnet连接上,输入字符回车,可以在控制台上看到输出
0 1
- java的selector用法
- Selector的用法
- android selector的用法
- selector的用法
- selector的用法
- Android的selector用法
- android:selector的用法
- android的selector用法
- 安卓中selector的用法
- Selector的用法
- selector的最佳用法
- Selector的用法
- android selector的用法
- selector的具体用法
- css selector的IE用法
- Android中的Selector的用法
- Android中selector的用法
- Android中的Selector的用法
- 清除浮动的原理和方法
- JavaScript小特性(6)——函数式编程
- 文字的颗粒效果
- Newtonsoft.Json.4.5.11使用方法总结---反序列化json字符串
- 从简单实例开始,学会写Makefile(二)
- java的selector用法
- 求二叉树的任意两个节点的最近公共祖先
- 【LeetCode】-Remove Duplicates from Sorted List
- HahaMil数据库(数据库操作组件)
- Longest Common Prefix
- 查找优化
- Android中文API(122) —— AudioRecord
- hdu 4569 TSP
- linux进程间通信-信号通信