package org.zwl.test.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger; /**
* @author zhenweiliu created on 3/6/14
*/
public class NioServer { private Selector selector;
private AtomicInteger acceptCount = new AtomicInteger(0);
private AtomicInteger readCount = new AtomicInteger(0);
private AtomicInteger writeCount = new AtomicInteger(0); public NioServer(int port) throws IOException {
ServerSocketChannel ssc = ServerSocketChannel.open(); // 使用默认的selector provider生成一个channel
ssc.configureBlocking(false); // 设置为非阻塞模式
ssc.bind(new InetSocketAddress(port)); // 绑定到端口
selector = Selector.open();
ssc.register(selector, SelectionKey.OP_ACCEPT); // 将channel注册到selector, 并指定其兴趣集事件 while (selector.select() > 0) { // select()是个阻塞方法, 只有当selector中有已经准备好的事件的时候才会返回, 它返回已经准备好的事件数
for (SelectionKey key : selector.selectedKeys()) { // 遍历已经准备好的事件的key
selector.selectedKeys().remove(key);
if (key.isAcceptable()) {
System.out.println("Accept client " + acceptCount.incrementAndGet());
SocketChannel sc = ((ServerSocketChannel) key.channel()).accept();
sc.configureBlocking(false);
sc.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE);
} else if (key.isReadable()) {
System.out.println("Read client " + readCount.incrementAndGet());
SocketChannel sc = (SocketChannel) key.channel();
ByteBuffer bb = ByteBuffer.allocate(1024);
bb.clear();
try {
while (sc.read(bb) > 0) {
bb.flip();
System.out.print(StandardCharsets.UTF_8.decode(bb));
bb.clear();
}
System.out.println();
} catch (IOException e) {
sc.close();
} if (key.isWritable()) {
System.out.println("Write client " + writeCount.incrementAndGet());
bb.clear();
bb.put("Hello Client".getBytes());
bb.flip();
try {
while (bb.hasRemaining()) {
sc.write(bb);
}
} catch (Exception e) {
sc.close();
}
}
}
}
}
} public static void main(String[] args) throws IOException {
new NioServer(8089);
}
}
package org.zwl.test.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
import java.util.concurrent.ExecutionException; import static java.nio.channels.SelectionKey.OP_CONNECT;
import static java.nio.channels.SelectionKey.OP_READ;
import static java.nio.channels.SelectionKey.OP_WRITE; /**
* @author zhenweiliu created on 3/6/14
*/
public class NioClient { public NioClient(String host, int port) throws Exception {
Selector selector = Selector.open();
SocketChannel sc = SocketChannel.open(new InetSocketAddress(host, port));
sc.configureBlocking(false);
SelectionKey sk = sc.register(selector, OP_CONNECT | OP_READ | OP_WRITE);
new ClientWriteThread(sk).start();
while (selector.select() > 0) {
for (SelectionKey key : selector.selectedKeys()) {
selector.selectedKeys().remove(key);
SocketChannel sc2 = (SocketChannel) key.channel();
if (key.isConnectable()) {
System.out.println("Connection established");
} else if (key.isReadable()) {
System.out.println("Read Server");
ByteBuffer bb = ByteBuffer.allocate(128);
bb.clear();
while (sc2.read(bb) > 0) {
bb.flip();
System.out.print(StandardCharsets.UTF_8.decode(bb));
bb.clear();
}
System.out.println();
}
}
}
} private static class ClientWriteThread extends Thread { private SelectionKey key; private Scanner scanner = new Scanner(System.in); public ClientWriteThread(SelectionKey key) {
this.key = key;
} @Override
public void run() {
while (key.isWritable()) {
String msg = scanner.nextLine();
ByteBuffer bb = ByteBuffer.allocate(msg.getBytes().length);
bb.clear();
bb.put(msg.getBytes());
bb.flip();
try {
while (bb.hasRemaining()) {
((SocketChannel) key.channel()).write(bb);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
} public static void main(String[] args) throws Exception {
new NioClient("localhost", 8089); }
}

使用selector的好处在于

1. selector可以使用一个线程同时监听多个channel,他们的事件准备由系统通知, 类似于事件驱动, 不需要自己手动缓存所有chanel, 并while去轮询, 这样效率高

2. 他的read write方法都是非阻塞方法, 返回的是本次操作写入(或读取)成功的数据字节数

Java NIO Test Case的更多相关文章

  1. 源码分析netty服务器创建过程vs java nio服务器创建

    1.Java NIO服务端创建 首先,我们通过一个时序图来看下如何创建一个NIO服务端并启动监听,接收多个客户端的连接,进行消息的异步读写. 示例代码(参考文献[2]): import java.io ...

  2. Five ways to maximize Java NIO and NIO.2--reference

    Java NIO -- the New Input/Output API package-- was introduced with J2SE 1.4 in 2002. Java NIO's purp ...

  3. 【NIO】Java NIO之选择器

    一.前言 前面已经学习了缓冲和通道,接着学习选择器. 二.选择器 2.1 选择器基础 选择器管理一个被注册的通道集合的信息和它们的就绪状态,通道和选择器一起被注册,并且选择器可更新通道的就绪状态,也可 ...

  4. java NIO详解

    http://zalezone.cn/2014/09/17/NIO%E7%B2%BE%E7%B2%B9/ 1. 前言 我们在写java程序的时候,为了进行优化,把全部的精力用在了处理效率上,但是对IO ...

  5. Java NIO之选择器

    1.简介 前面的文章说了缓冲区,说了通道,本文就来说说 NIO 中另一个重要的实现,即选择器 Selector.在更早的文章中,我简述了几种 IO 模型.如果大家看过之前的文章,并动手写过代码的话.再 ...

  6. android升级后错误:Unable to execute dex: java.nio.BufferOverflowException.Check

    Android SDK Tools升级为22.3,Android SDK Platform-tools 升级为19后,编译工程出现错误: Unable to execute dex: java.nio ...

  7. IO的详细解释:It's all about buffers: zero-copy, mmap and Java NIO

    There are use cases where data need to be read from source to a sink without modification. In code t ...

  8. Java NIO Files

    Java NIO Files Files.exists() Files.createDirectory() Files.copy() Overwriting Existing Files Files. ...

  9. java NIO Buffer 详解(1)

    1.java.io  最为核心的概念是流(stream),面向流的编程,要么输入流要么输出流,二者不可兼具: 2.java.nio 中拥有3个核心概念: Selector Channel, Buffe ...

随机推荐

  1. P2690 接苹果

    P2690 接苹果f[i][j][k]表示i=1或2,表示j时刻cow在哪棵树下j表示时刻k表示转移了k次如果当前第1棵树落苹果f[1][t][left]=max(f[1][t-1][left],f[ ...

  2. strlen()和mb_strlen()

    换行需要用双引号,单引号只会输出字符. strlen()返回字符串所占的字节数,对于utf8编码的中文,一个汉字占三个字节. mb_strlen()返回字符个数,如果不写第二个参数,就会使用内部编码, ...

  3. BZOJ.2806.[CTSC2012]Cheat(广义后缀自动机 DP 单调队列)

    题目链接 首先二分答案L.然后就是判断能否将原串划分出一些长度不小于L的子串,这些子串要是给定n个串中的某个串的子串,且满足它们的长度之和不小于原串长度的90%. 贪心多长选一段什么的显然不对.老老实 ...

  4. 查看APP的下载量

    开发者账号登陆后:→用户中心→iTunes Connect→Sales Trend

  5. Emmet 语法介绍

    一.生成 HTML 文档初始结构 HTML 文档的初始结构,就是包括 doctype.html.head.body 以及 meta 等内容.你只需要输入一个 “!” 就可以生成一个 HTML5 的标准 ...

  6. logstash grok 分割匹配日志

    使用logstash的时候,为了更细致的切割日志,会写一些正则表达式. 使用方法 input { file { type => "billin" path => &qu ...

  7. FireDAC 下的 Sqlite [2] - 第一个例子

    为了方便测试, 我把官方提供的 C:\Users\Public\Documents\Embarcadero\Studio\14.0\Samples\data\FDDemo.sdb 复制了一份到 C:\ ...

  8. LayoutInflater作用及使用(转)

    作用: 1.对于一个没有被载入或者想要动态载入的界面, 都需要使用inflate来载入. 2.对于一个已经载入的Activity, 就可以使用实现了这个Activiyt的的findViewById方法 ...

  9. Redis "MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk"问题的解决(转)

    今天第二次遇到Redis “MISCONF Redis is configured to save RDB snapshots, but is currently not able to persis ...

  10. C++学习笔记23,类内函数重载

    该博文仅用于交流学习.请慎用于不论什么商业用途,本博主保留对该博文的一切权利. 博主博客:http://blog.csdn.net/qq844352155 转载请注明出处: 在一个类内,最常见的就是构 ...