前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【NIO服务器代码-可测试】

【NIO服务器代码-可测试】

作者头像
用户5640963
发布2020-03-19 09:49:27
8970
发布2020-03-19 09:49:27
举报
文章被收录于专栏:卯金刀GG卯金刀GG

1、目的:了解NIO服务的工作原理

2、代码:

代码语言:javascript
复制
/**
 * @Author: Liu
 * @Descripition:NIO测试
 * @Date; Create in 2020/3/14 16:10
 **/
public class NIOServerDemo {

    private int port = 8080;

    private Selector selector;

    private ByteBuffer buffer = ByteBuffer.allocate(1024);

    //初始化完毕
    public NIOServerDemo(int port) {

        this.port = port;
        try {
            ServerSocketChannel server = ServerSocketChannel.open();
            server.bind(new InetSocketAddress(this.port));
            //设置非阻塞
            server.configureBlocking(false);

            selector = Selector.open();
            server.register(selector, SelectionKey.OP_ACCEPT);


        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void listen() {
        System.out.println("listen on " + this.port + ".");

        try {
            while (true) {
                selector.select();

                Set<SelectionKey> selectionKeys = selector.selectedKeys();

                Iterator<SelectionKey> iterator = selectionKeys.iterator();

                while (iterator.hasNext()) {
                    SelectionKey key = iterator.next();

                    iterator.remove();
                    //数据就绪,数据可读,可写等 看key的方法
                    process(key);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    //具体办理业务的方法,处理业务逻辑
    //每次轮询调用一次,同步
    private void process(SelectionKey key) throws IOException {
        //针对每一种状态有一个反应
        if (key.isAcceptable()) {
            ServerSocketChannel channel = (ServerSocketChannel) key.channel();
            //体现非阻塞,返回一个状态和反馈
            SocketChannel accept = channel.accept();
            channel.configureBlocking(false);
            key = channel.register(selector, SelectionKey.OP_READ);

        } else if (key.isReadable()) {
            SocketChannel channel = (SocketChannel) key.channel();
            int read = channel.read(buffer);
            if (read > 0) {
                buffer.flip();

                String content = new String(buffer.array(), 0, read);
                SelectionKey register = channel.register(selector, SelectionKey.OP_READ);
                register.attach(content);
                System.out.println("读取内容:" + content);

            }
        } else if (key.isWritable()) {
            SocketChannel channel = (SocketChannel) key.channel();
            String content = (String) key.attachment();
            channel.write(ByteBuffer.wrap(("输出:" + content).getBytes()));
            channel.close();
        }

    }

    public static void main(String[] args) {
        new NIOServerDemo(8080).listen();
    }
}

每天提高一点点

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档