Java NIO Reactor模式
来源:互联网 发布:微pe windows安装器 编辑:程序博客网 时间:2024/05/12 00:39
- package com.zzq.nio.reactor;
- import java.io.IOException;
- import java.net.InetSocketAddress;
- import java.nio.ByteBuffer;
- import java.nio.channels.SelectionKey;
- import java.nio.channels.Selector;
- import java.nio.channels.ServerSocketChannel;
- import java.nio.channels.SocketChannel;
- import java.util.Iterator;
- public class Reactor implements Runnable {
- private ServerSocketChannel serverSocketChannel = null;
- private Selector selector = null;
- public Reactor() {
- try {
- selector = Selector.open();
- serverSocketChannel = ServerSocketChannel.open();
- serverSocketChannel.configureBlocking(false);
- serverSocketChannel.socket().bind(new InetSocketAddress(8888));
- SelectionKey selectionKey = serverSocketChannel.register(selector,
- SelectionKey.OP_ACCEPT);
- selectionKey.attach(new Acceptor());
- System.out.println("服务器启动正常!");
- } catch (IOException e) {
- System.out.println("启动服务器时出现异常!");
- e.printStackTrace();
- }
- }
- public void run() {
- while (true) {
- try {
- selector.select();
- Iterator<SelectionKey> iter = selector.selectedKeys().iterator();
- while (iter.hasNext()) {
- SelectionKey selectionKey = iter.next();
- dispatch((Runnable) selectionKey.attachment());
- iter.remove();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- public void dispatch(Runnable runnable) {
- if (runnable != null) {
- runnable.run();
- }
- }
- public static void main(String[] args) {
- new Thread(new Reactor()).start();
- }
- class Acceptor implements Runnable {
- public void run() {
- try {
- SocketChannel socketChannel = serverSocketChannel.accept();
- if (socketChannel != null) {
- System.out.println("接收到来自客户端("
- + socketChannel.socket().getInetAddress().getHostAddress()
- + ")的连接");
- new Handler(selector, socketChannel);
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- class Handler implements Runnable {
- private static final int READ_STATUS = 1;
- private static final int WRITE_STATUS = 2;
- private SocketChannel socketChannel;
- private SelectionKey selectionKey;
- private int status = READ_STATUS;
- public Handler(Selector selector, SocketChannel socketChannel) {
- this.socketChannel = socketChannel;
- try {
- socketChannel.configureBlocking(false);
- selectionKey = socketChannel.register(selector, 0);
- selectionKey.interestOps(SelectionKey.OP_READ);
- selectionKey.attach(this);
- selector.wakeup();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public void run() {
- try {
- if (status == READ_STATUS) {
- read();
- selectionKey.interestOps(SelectionKey.OP_WRITE);
- status = WRITE_STATUS;
- } else if (status == WRITE_STATUS) {
- process();
- selectionKey.cancel();
- System.out.println("服务器发送消息成功!");
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public void read() throws IOException {
- ByteBuffer buffer = ByteBuffer.allocate(1024);
- socketChannel.read(buffer);
- System.out.println("接收到来自客户端(" + socketChannel.socket().getInetAddress().getHostAddress()
- + ")的消息:" + new String(buffer.array()));
- }
- public void process() throws IOException {
- String content = "Hello World!";
- ByteBuffer buffer = ByteBuffer.wrap(content.getBytes());
- socketChannel.write(buffer);
- }
- }
0 0
- Java NIO Reactor模式
- java NIO模式之Reactor
- Java NIO 反应器(Reactor)模式
- reactor模式与java nio
- java NIO和Reactor模式
- Java NIO 反应器(Reactor)模式
- Reactor 模式的 JAVA NIO 多线程服务器
- java NIO的多路复用及reactor模式
- 从java-nio到reactor设计模式
- Java NIO与反应器(reactor)模式
- Reactor模式和NIO
- Reactor模式和NIO
- Reactor模式和NIO
- Reactor模式和NIO
- Reactor模式和NIO
- Reactor模式和NIO
- Reactor模式和NIO
- Reactor模式和NIO
- Java NIO 反应器(Reactor)模式
- 三分钟破解无线网——无线网络安全攻防
- 用法总结:NSArray,NSSet,NSDictionary
- 软件测试概要
- c++回调函数 callback
- Java NIO Reactor模式
- margin和padding
- 关于BitBlaze的中文介绍
- 数据源与连接池
- python中re正则表达式模块学习
- android项目中关于proguard.cfg和proguard-project.txt 混淆文件的用法与配置
- 判断浏览器
- ofbiz 中文乱码问题解决
- 若秋叶静离枝淡对离别