• NIO记录


    小demo:

     服务器端代码

    package demoNio;
    
    import com.oracle.nio.BufferSecrets;
    
    import java.io.IOException;
    import java.net.InetSocketAddress;
    import java.nio.Buffer;
    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;
    import java.util.Set;
    
    public class NioServerWithSelector {
        public static void main(String[] args) throws IOException {
            //获得服务器断点的通道
            ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
            //绑定端口
            serverSocketChannel.socket().bind(new InetSocketAddress(6666));
            //设置成异步
            serverSocketChannel.configureBlocking(false);
            //创建Selector,Selector是个监控器
            Selector selector = Selector.open();
            //注册(ops参数为连接)
            serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
            while(true){
                //2秒之内没人连接
                while(selector.select(2000) == 0){
                    System.out.println("等待连接");
                    continue;
                }
                //获得准备就绪的keys
                Set<SelectionKey> keys = selector.selectedKeys();
                //遍历keys
                Iterator<SelectionKey> iterator = keys.iterator();
                while(iterator.hasNext()){
                    //获得key
                    SelectionKey key = iterator.next();
    
                    //如果是连接事件
                    if(key.isAcceptable()){
                        //第一次连接,获得通道
                        SocketChannel socketChannel = serverSocketChannel.accept();
                        if(socketChannel != null){
                            System.out.println(Thread.currentThread().getName()+" : 接收到一个连接 "+ socketChannel.getRemoteAddress() +"发送的端口是:" + socketChannel.getRemoteAddress());
                            //设置管道异步
                            socketChannel.configureBlocking(false);
                            //注册
                            socketChannel.register(selector,SelectionKey.OP_READ, ByteBuffer.allocate(1024));
                        }
                    }
                    //如果是读事件
                    if(key.isReadable()){
                        //获得通道,直接通过key得到!!
                        SocketChannel socketChannel = (SocketChannel) key.channel();
                        //获取缓冲区
                        ByteBuffer buffer = (ByteBuffer)key.attachment();
                        int length = socketChannel.read(buffer);
                        if(length != -1){
                            System.out.println(Thread.currentThread().getName()+" : 接收到的数据是 :"+ new String(buffer.array(),0,length));
                        }
                        //清空缓存区
                        buffer.clear();
                    }
    
    
                    iterator.remove();
                }
            }
        }
    }

    客户端代码:

    package demoNio;
    
    import java.io.IOException;
    import java.net.InetAddress;
    import java.net.InetSocketAddress;
    import java.net.Socket;
    
    public class NioClient {
        public static void main(String[] args) throws IOException {
            InetSocketAddress socketAddress = new InetSocketAddress(InetAddress.getLocalHost(),6666);
            Socket socket = new Socket();
            socket.connect(socketAddress);
            socket.getOutputStream().write("hello".getBytes());
        }
    }
  • 相关阅读:
    约瑟夫问题
    [bzoj] 2049 洞穴勘探 || LCT
    [bzoj] 1597 土地购买 || 斜率优化dp
    [usaco] 2008 Dec Largetst Fence 最大的围栏 2 || dp
    [LNOI] 相逢是问候 || 扩展欧拉函数+线段树
    [bzoj] 1588 营业额统计 || Splay板子题
    [hdu] 5115 Dire Wolf || 区间dp
    [poj] 1651 Multiplication Puzzle || 区间dp
    [bzoj] 1090 字符串折叠 || 区间dp
    [bzoj] 1068 压缩 || 区间dp
  • 原文地址:https://www.cnblogs.com/Esquecer/p/12514658.html
Copyright © 2020-2023  润新知