NIO记录

 

 

 

NIO记录

 

 

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

 

上一篇:Netty 源码解析(二):Netty 的 Channel


下一篇:《House of Cards》观后感