一个简单的NIO实例
2011-09-26 17:07
176 查看
public class TestNio {
public static void main(String[] args) throws IOException {
Selector selector = Selector.open();
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.socket().bind(new InetSocketAddress("192.168.27.48", 7777));
ssc.configureBlocking(false);
ssc.register(selector, SelectionKey.OP_ACCEPT);
while(true){
int n = selector.select();
if(n > 0){
Iterator<SelectionKey> ite = selector.selectedKeys().iterator();
while(ite.hasNext()){
SelectionKey k = ite.next();
ite.remove();
if(k.isAcceptable()){
SocketChannel s = ((ServerSocketChannel)k.channel()).accept();
s.configureBlocking(false);
System.out.println("accept:" + s.socket().getPort() + " s:" + s);
s.register(selector, SelectionKey.OP_WRITE);
}else if(k.isReadable()){
SocketChannel sc = (SocketChannel) k.channel();
ByteBuffer dst = ByteBuffer.allocate(1024);
sc.read(dst);
System.out.println("str:" + dst.toString());
sc.keyFor(selector).cancel();
sc.keyFor(selector).interestOps(SelectionKey.OP_WRITE);
}else if(k.isWritable()){
SocketChannel sc = (SocketChannel) k.channel();
ByteBuffer b = ByteBuffer.allocate(12);
b.put(new String("repaly").getBytes("utf-8"));
b.flip();
sc.write(b);
sc.keyFor(selector).interestOps(SelectionKey.OP_READ);
}
}
}
}
}
}
public static void main(String[] args) throws IOException {
Selector selector = Selector.open();
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.socket().bind(new InetSocketAddress("192.168.27.48", 7777));
ssc.configureBlocking(false);
ssc.register(selector, SelectionKey.OP_ACCEPT);
while(true){
int n = selector.select();
if(n > 0){
Iterator<SelectionKey> ite = selector.selectedKeys().iterator();
while(ite.hasNext()){
SelectionKey k = ite.next();
ite.remove();
if(k.isAcceptable()){
SocketChannel s = ((ServerSocketChannel)k.channel()).accept();
s.configureBlocking(false);
System.out.println("accept:" + s.socket().getPort() + " s:" + s);
s.register(selector, SelectionKey.OP_WRITE);
}else if(k.isReadable()){
SocketChannel sc = (SocketChannel) k.channel();
ByteBuffer dst = ByteBuffer.allocate(1024);
sc.read(dst);
System.out.println("str:" + dst.toString());
sc.keyFor(selector).cancel();
sc.keyFor(selector).interestOps(SelectionKey.OP_WRITE);
}else if(k.isWritable()){
SocketChannel sc = (SocketChannel) k.channel();
ByteBuffer b = ByteBuffer.allocate(12);
b.put(new String("repaly").getBytes("utf-8"));
b.flip();
sc.write(b);
sc.keyFor(selector).interestOps(SelectionKey.OP_READ);
}
}
}
}
}
}
相关文章推荐
- 一个Java的SWT菜单显示和递归调用的简单实例
- 微信企业号会议助手---一个简单的SSM实例
- Tensorflow的一个简单实例,线性回归
- C语言经典实例008:一个简单的求和程序
- 一个简单的php MVC实例
- Java review--NIO实例:实现服务端和客户端的简单通信
- 自己写的一个操作Mysql的简单的实例
- 一个简单Struts实例
- vue2:一个简单实例
- php和数据库结合的一个 简单的web实例 代码分析 (php初学者)
- 用spring+springMvc完成的一个简单的登陆实例
- linux socke编程实例:一个简单的echo服务器程序(2)
- 本文实现了一个基于servlet技术的简单的csv文件导出的程序实例。
- XML DOM解析(Java)的一个简单实例
- php+jquery+ajax+json的一个最简单实例
- jquery实现的一个简单进度条效果实例
- 一个Ajax简单实例分析
- php实现的一个简单json rpc框架实例
- SpringMVC4+thymeleaf3的一个简单实例(篇三:页面参数获取)
- java swing最简单实例(1) 一个空的JFrame