资讯详情

NIO单线程网络传输对象

package com.xtkj.test2;   import com.xtkj.vo.Employee;  import java.io.ByteArrayOutputStream; import java.io.ObjectOutputStream; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.*; import java.util.Iterator; import java.util.Set;  public class NioServer1 {     public static void main(String[] args) throws Exception {         new NioServer1().init();     }      /**      * 初始化      * @throws Exception      */     public void init() throws Exception {         /**          * 获取通道          */         ServerSocketChannel ssChannel = ServerSocketChannel.open();         /**          * 绑定端口          */         ssChannel.bind(new InetSocketAddress(9999));         /**          * 设置非阻塞          */         ssChannel.configureBlocking(false);         /**          * 启动          */         start(ssChannel);     }      public void start(ServerSocketChannel ssChannel) throws Exception {         /**          * 获取事件管理          */         Selector selector = Selector.open();         /**          * 注册监听          */         ssChannel.register(selector, SelectionKey.OP_ACCEPT);          while (true) {             /**              * 所有注册事件              */             Set<SelectionKey> keys = selector.selectedKeys();             Iterator<SelectionKey> ite = keys.iterator();             /**              * 轮询监听              */             int select = selector.select();             while (ite.hasNext()) {                 /**                  * 获取当前监听事件对象                  */                 SelectionKey next = ite.next();                 /**                  * 防止事件重复,所有执行完成后,删除当前事件                  */                 ite.remove();                 /**                  * 判断是否连接                  */                 if (next.isAcceptable()) {                     /**                      * 如果有进入,立即获得通道                      */                     SocketChannel accept = ssChannel.accept();                     /**                      * 设置SocketChannel 为非阻塞                      */                     accept.configureBlocking(false);                     /**                      * 在将 在事件中阅读事件set中。                      */                     accept.register(selector, SelectionKey.OP_READ);                 }                 /**                  * 如果当前的时间对象是阅读                  */                 if (next.isReadable()) {                     /**                      * 获取通道                      */                     SocketChannel channel = (SocketChannel) next.channel();                     /**                      * 执行阅读方法                      */                     read(channel);                     /**                      * 在设置写作事件之前set里面                      */                     next.interestOps(SelectionKey.OP_WRITE);                 }                 /**                  * 若为事件对象                  */                 if (next.isWritable()) {                     /**                      * 获取通道                      */                     SocketChannel channel = (SocketChannel) next.channel();                     /**                      *                      */                     writeFunction(channel);                     /**                      *                      */                     next.interestOps(SelectionKey.OP_READ);                 }             }         }     }      /**      * 写入对象      * @param channel      * @throws Exception      */     private void writeFunction(SocketChannel channel) throws Exception {         ByteArrayOutputStream bout = new ByteArrayOutputStream();         ObjectOutputStream obj = new ObjectOutputStream(bout);         Code code = new Code();         code.setCode(1);         code.setEmployee(new Employee(1, "admin", "男", "湖北咸宁"));         obj.writeObject(code);         obj.flush();         byte[] bytes = bout.toByteArray();          ByteBuffer writebuff = ByteBuffer.wrap(bytes);         channel.write(writebuff);     }       private void read(SocketChannel socketChannel) throws Exception {         /**          * 创建缓存对象          */         ByteBuffer readBuff = ByteBuffer.allocate(1024);         /**          * 因为它可能会被重复,所以阅读指南 可以Clear 目前创造了新的 所以。。。。。          */         readBuff.clear();         /**          * 读取通道中的数据 缓存中          */         socketChannel.read(readBuff);         /**          * 设置读模式          */         readBuff.flip();         /**          * 调试          */         System.out.println("readBuff.position() = "   readBuff.position());         System.out.println("readBuff.limit() = "   readBuff.limit());         System.out.println("readBuff.capacity() = "   readBuff.capacity());         String val = new String(readBuff.array(), 0, readBuff.limit());         System.out.println("val = "   val);         switch (val) {             case "1":                 System.out.println("1");                break;
            case "2":
                System.out.println("2");
                break;
            case "3":
                System.out.println("3");
                break;
            case "4":
                System.out.println("4");
                break;
        }
    }
}

 

标签: xtkj智能传感器

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

 锐单商城 - 一站式电子元器件采购平台  

 深圳锐单电子有限公司