Java中NioSocket的用法--Spring MVC
来源:互联网 发布:遗传算法的流程图 编辑:程序博客网 时间:2024/06/06 00:20
package com.test2;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.Iterator;public class NIOServer {public static void main(String[] args) throws Exception{ //创建ServerSocketChannel,监听8082端口 ServerSocketChannel ssc = ServerSocketChannel.open(); ssc.socket().bind(new InetSocketAddress(8082)); //设置为非阻塞模式 ssc.configureBlocking(false); //为ssc注册选择器 Selector selector = Selector.open(); ssc.register(selector, SelectionKey.OP_ACCEPT); //创建处理器 Handler handler = new Handler(1024); while (true) { //等待请求,每次等待阻塞3s,超过3s后线程继续向下运行,如果传入0或者不传参数将一直阻塞 if (selector.select(3000) == 0) { System.out.println("等待请求超时。。。。。。。。。"); continue; } System.out.println("处理请求。。。。。"); //获取待处理的SelectionKey Iterator<SelectionKey> keyIter = selector.selectedKeys().iterator(); while (keyIter.hasNext()) { SelectionKey key = keyIter.next(); try { //接收到链接请求时 if (key.isAcceptable()) { handler.handleAccept(key); } //读数据 if (key.isReadable()) { handler.handleRead(key); } } catch (Exception e) { // TODO: handle exception keyIter.remove(); continue; } } } }}
package com.test2;import java.io.IOException;import java.nio.ByteBuffer;import java.nio.channels.SelectionKey;import java.nio.channels.ServerSocketChannel;import java.nio.channels.SocketChannel;import java.nio.charset.Charset;public class Handler { private int bufferSize=1024; private String localCharset="UTF-8"; public Handler() { } public Handler(int bufferSize) { this(bufferSize,null); } public Handler(int bufferSize, String localCharset) { // TODO Auto-generated constructor stub if(bufferSize>0) this.bufferSize=bufferSize; if(localCharset!=null) this.localCharset=localCharset; } public void handleAccept(SelectionKey key) throws IOException{ SocketChannel sc=((ServerSocketChannel) key.channel()).accept(); sc.configureBlocking(false); sc.register(key.selector(), SelectionKey.OP_READ,ByteBuffer.allocate(bufferSize)); } public void handleRead(SelectionKey key) throws IOException{ //获取channel SocketChannel sc=(SocketChannel) key.channel(); //获取buffer并重置 ByteBuffer buffer=(ByteBuffer) key.attachment(); buffer.clear(); //没有读到内容则关闭 if(sc.read(buffer)==-1) { sc.close(); }else { //将buffer转换为读状态 buffer.flip(); //将buffer中接收到的值按localCharset格式编码后保存在receivedString String receivedString=Charset.forName(localCharset).newDecoder().decode(buffer).toString(); System.out.println("receive from client: "+receivedString); //返回数据给客户端 String sendString ="receive data: "+receivedString; buffer=ByteBuffer.wrap(sendString.getBytes(localCharset)); sc.write(buffer); sc.close(); } }}
阅读全文
1 0
- Java中NioSocket的用法--Spring MVC
- Java NioSocket 的用法
- java中niosocket的实例使用
- Java中Socket的用法--Spring MVC
- Spring MVC中各个filter的用法
- Spring MVC中各个filter的用法
- Spring MVC 中Session的用法
- Spring MVC中各个filter的用法
- java niosocket示例
- java NIOSocket 通信简要举例
- Java 网络编程之NioSocket
- Spring MVC中Session的正确用法之我见
- Spring MVC中Session的正确用法之我见
- Spring MVC中Session的正确用法之我见
- Spring MVC的简单用法
- Spring MVC RedirectAttributes的用法
- spring mvc+spring mobile简单的用法
- spring mvc+spring mobile简单的用法
- Electron实现使用Sqlite3库
- 如何在初始化窗体不是最大化情况下,获取最大化窗体的客户区的尺寸
- 时间同步:适用于国内的 NTP 服务器地址,可用于时间同步或 Android 加速 GPS 定位
- ACM自用常用模板(c++版)
- Mysql数据库与sql语言(三)--常用函数
- Java中NioSocket的用法--Spring MVC
- python多维数组切片
- 为什么精英都是清单控(笔记)——数字化清单
- 【Spring Task】定时任务详解实例-@Scheduled
- spring @Async
- easyui手风琴配合tree 加tab 的数据库动态添加
- java IO笔记(IO简述)
- ZooKeeper入门
- IT行业中BA(Business System Analyst)角色怎么样?以及职责