服务器机制模拟(NIO)
来源:互联网 发布:网络性能评测指标 编辑:程序博客网 时间:2024/06/01 11:49
最近学习服务器,研究下Socket,初步流程代码(待整理)
package NIOTest;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.nio.charset.Charset;import java.util.HashMap;import java.util.Map;import java.util.Set;/** * Created by Yu on 2017/1/20. * Desc: */public class EscoServer { public static void main(String[] args) throws IOException { ServerSocketChannel serverSocketChannel=ServerSocketChannel.open(); Selector selector=Selector.open(); serverSocketChannel.socket().bind(new InetSocketAddress(9999)); serverSocketChannel.configureBlocking(false); serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); HashMap<Integer,SocketChannel> session=new HashMap<>(); int useSessionId=1; ByteBuffer buffer=ByteBuffer.allocate(1000); while (true) { if(selector.select()<0) continue; Set<SelectionKey> selected=selector.selectedKeys(); for(SelectionKey selectionKey :selected) { if (selectionKey.isAcceptable()) { ServerSocketChannel server=(ServerSocketChannel)selectionKey.channel(); SocketChannel clientChannel=server.accept(); Integer sessionId=useSessionId++; session.put(sessionId,clientChannel); clientChannel.configureBlocking(false); clientChannel.register(selector,SelectionKey.OP_READ,sessionId); buffer.clear(); buffer.put(ByteBuffer.wrap("Success\n".getBytes())); buffer.flip(); clientChannel.write(buffer); } else if(selectionKey.isReadable()) { SocketChannel client=(SocketChannel)selectionKey.channel(); Integer sessionId=(Integer) selectionKey.attachment(); buffer.clear(); long byteRead=client.read(buffer); System.out.print("Over"); System.out.print(byteRead); System.out.print("\n"); buffer.flip(); if(-1==byteRead) { System.out.print(client.getRemoteAddress().toString()+"......Close"); session.remove(sessionId); client.close(); } else if(byteRead>2) { System.out.print(byteRead); String receivedString=Charset.forName("UTF-8").newDecoder().decode(buffer).toString(); System.out.print("Read:"); System.out.print(receivedString); System.out.print("\n"); if(session.size()>1) { for (SocketChannel channel :session.values()) { if(client !=channel) { buffer.clear(); buffer.put(ByteBuffer.wrap(receivedString.getBytes())); buffer.flip(); channel.write(buffer); } } } } } } selected.clear(); } }}
0 0
- 服务器机制模拟(NIO)
- nio 模拟http服务器
- NIO服务器
- NIO机制初探
- NIO机制总结
- java NIO工作机制
- Java NIO机制
- Java NIO工作机制
- NIO服务器框架
- NIO服务器框架
- NIO 简单http 服务器
- Java NIO服务器实例
- Java NIO服务器实例
- JAVA NIO 服务器(三)
- 【Java】BIO NIO 服务器
- 模拟服务器
- Java NIO-锁机制Lock
- Kafka的NIO通信机制
- tomcat源码(四) Container启动
- Intent中的四个重要属性——Action、Data、Category、Extras
- C++——KMP算法例题——Oulipo(POJ3461)
- 小记
- Java基本概念-错误页面的配置
- 服务器机制模拟(NIO)
- jquery,js,checkbox多选框复选框取值和赋值
- Java synchronized关键字用法(清晰易懂)
- Android Studio 安装应用失败总结
- 设计模式之观察者模式
- ssh框架项目下的购物车模块
- c++ prime 课后题 文本查询程序
- AngularJs checkbox绑定
- Find All Numbers Disappeared in an Array