netty 20-30

主要是Selector 有几个方法
这个就是类似一个监听器
selector 方法,就是阻塞监听 channel上的变化 ,有变化的都返回 selectkeys
有等确定事件的方法
now 有立刻返回的方法

利用NIO写一个Server 和 Client 的通讯

package nio;

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.util.Iterator;
import java.util.Set;

public class NServer {
    public static void main(String[] args) throws Exception {
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        Selector selector = Selector.open();
        serverSocketChannel.socket().bind(new InetSocketAddress(6677));
        serverSocketChannel.configureBlocking(false);
        SelectionKey selectionKey = serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);



        while (true) {
            int select = selector.select(1000);
            if (select == 0) {
//                System.out.println("服务器等待了一秒");
                continue;

            }
//用select 监听到了一个事件。  然后把注册的所有key ,遍历一下,看有没有跟监听的一样的。如果有,我们就做相对应的处理
            Set<SelectionKey> selectionKeys = selector.selectedKeys();
            Iterator<SelectionKey> keyIterator = selectionKeys.iterator();
            while (keyIterator.hasNext()) {
                SelectionKey ke = keyIterator.next();
                if (ke.isAcceptable()) {
                    ServerSocketChannel serverSocketChannel1 = (ServerSocketChannel) ke.channel();
                    SocketChannel socketChannel = serverSocketChannel1.accept();
                    socketChannel.configureBlocking(false);
                    System.out.println("服务端收到了"+": "+socketChannel.hashCode());
                    socketChannel.register(selector, SelectionKey.OP_READ, ByteBuffer.allocate(1024));
                }
                if (ke.isReadable()) {
                    SocketChannel socketChannel = (SocketChannel) ke.channel();
//                    socketChannel.configureBlocking(false);
                    ByteBuffer byteBuffer = (ByteBuffer) ke.attachment();
                    socketChannel.read(byteBuffer);
                    System.out.println("new String(byteBuffer.arry) = " + new String(byteBuffer.array()));



                }
                keyIterator.remove();
            }

        }



    }
}

以上这个是Server

package nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

public class NClient {
    public static void main(String[] args) throws Exception {
        SocketChannel so = SocketChannel.open();
        so.configureBlocking(false);
        InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1", 6677);
        if (!so.connect(inetSocketAddress)) {
            while (!so.finishConnect()) {
                System.out.println("随便干点别的");
            }
        }
        String str = "Hello NIOServer";
        ByteBuffer buffer = ByteBuffer.wrap(str.getBytes());
        so.write(buffer);
        System.in.read();

    }
}

以上这个是Client

selector 的两个方法说明
selectionkeys 这个是发生事件 根据前端返回的事件,把注册的selectionkey放在keys 这两个属性都是set集合
keys 这个是注册

SelectionKey对象对应的api
能获取到selector
channel
attchment就是buffer

可以拿到支持的事件
isAccessAble
read
write

interestOps 改变
监听的事件

ServerSockChannel 对象的api
open()
bind()
configureBlocking
accept()
register()

SocketChannel
具体的读写操作都是这个对象的 api
open()
configureBlocking()
connect()
finishConjnect() 如果上面的连接失败了。我们继续连接
register 不过这个多了个缓存。 之前的只要select 和监听 的ops事件
read()写缓存
writer()写入Channel

发布了66 篇原创文章 · 获赞 0 · 访问量 795

猜你喜欢

转载自blog.csdn.net/Be_With_I/article/details/103976114
今日推荐