首页 > 其他 > 详细

NIO记录

时间:2020-03-18 00:51:24      阅读:82      评论:0      收藏:0      [点我收藏+]

 

 

 

技术分享图片

 

 

技术分享图片

 

 

小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());
    }
}

 

NIO记录

原文:https://www.cnblogs.com/Esquecer/p/12514658.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!