JavaNIO

作者: IAmWhoAmI | 来源:发表于2018-05-26 23:23 被阅读40次

javaNIO整体上看到更多是API怎么用。
buffer。channel。selector.
更细的API用的时候了解,这几个概念根据例子debug即可。

/**
 * Alipay.com Inc.
 * Copyright (c) 2004-2018 All Rights Reserved.
 */
package io;

import org.apache.bcel.generic.Select;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
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;
import java.util.concurrent.locks.LockSupport;

/**
 *
 * @author qz.zzm
 * @version $Id: NioTest.java, v 0.1 2018年05月22日 上午10:42 qz.zzm Exp $
 */
public class NioTest {
    /*标识数字*/
    private int        flag          = 0;
    /*缓冲区大小*/
    private int        BLOCK         = 4096;
    /*接受数据缓冲区*/
    private ByteBuffer sendbuffer    = ByteBuffer.allocate(BLOCK);
    /*发送数据缓冲区*/
    private ByteBuffer receivebuffer = ByteBuffer.allocate(BLOCK);
    private  Selector selector;

    // 监听
    public NioTest(int port) throws IOException {
        // 打开服务器套接字通道
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        // 服务器配置为非阻塞
        serverSocketChannel.configureBlocking(false);
        // 检索与此通道关联的服务器套接字
        ServerSocket serverSocket = serverSocketChannel.socket();
        // 进行服务的绑定
        serverSocket.bind(new InetSocketAddress(port));
        // 通过open()方法找到Selector
        selector = Selector.open();
        // 注册到selector,等待连接
        serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
        System.out.println("Server Start----8888:");
    }

    private void listen() throws IOException {
        while (true) {
            // 选择一组键,并且相应的通道已经打开
            selector.select();
            // 返回此选择器的已选择键集。
            Set<SelectionKey> selectionKeys = selector.selectedKeys();
            Iterator<SelectionKey> iterator = selectionKeys.iterator();
            while (iterator.hasNext()) {
                SelectionKey selectionKey = iterator.next();
                iterator.remove();
                handleKey(selectionKey);
            }
        }
    }

    // 处理请求
    private void handleKey(SelectionKey selectionKey) throws IOException {
        // 接受请求
        ServerSocketChannel server = null;
        SocketChannel client = null;
        String receiveText;
        String sendText;
        int count=0;
        // 测试此键的通道是否已准备好接受新的套接字连接。
        if (selectionKey.isAcceptable()) {
            // 返回为之创建此键的通道。
            server = (ServerSocketChannel) selectionKey.channel();
            // 接受到此通道套接字的连接。
            // 此方法返回的套接字通道(如果有)将处于阻塞模式。
            client = server.accept();
            // 配置为非阻塞
            client.configureBlocking(false);
            // 注册到selector,等待连接
            client.register(selector, SelectionKey.OP_READ);
        } else if (selectionKey.isReadable()) {
            // 返回为之创建此键的通道。
            client = (SocketChannel) selectionKey.channel();
            //将缓冲区清空以备下次读取
            receivebuffer.clear();
            //读取服务器发送来的数据到缓冲区中
            count = client.read(receivebuffer);
            if (count > 0) {
                receiveText = new String( receivebuffer.array(),0,count);
                System.out.println("服务器端接受客户端数据--:"+receiveText);
                client.register(selector, SelectionKey.OP_WRITE);
            }
        } else if (selectionKey.isWritable()) {
            //将缓冲区清空以备下次写入
            sendbuffer.clear();
            // 返回为之创建此键的通道。
            client = (SocketChannel) selectionKey.channel();
            sendText="message from server--" + flag++;
            //向缓冲区中输入数据
            sendbuffer.put(sendText.getBytes());
            //将缓冲区各标志复位,因为向里面put了数据标志被改变要想从中读取数据发向服务器,就要复位
            sendbuffer.flip();
            //输出到通道
            client.write(sendbuffer);
            System.out.println("服务器端向客户端发送数据--:"+sendText);
            client.register(selector, SelectionKey.OP_READ);
        }
    }

    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        int port = 8888;
        NioTest server = new NioTest(port);
        server.listen();
    }


}

可以telnet 127.0.0.1 8888
然后debug走下,大致知道如何使用。

Java NIO 这两篇文章讲了怎么用:
https://blog.csdn.net/suifeng3051/article/details/48160753
https://blog.csdn.net/suifeng3051/article/details/48441629
然后可以围观下,这个也是类似的文章:
http://developer.51cto.com/art/201112/307685.htm
更详细javaNIO的API:
http://tutorials.jenkov.com/java-nio/index.html

然后了解下 底层的select(),poll(),epoll(),了解下就好,除非要用到:
下面的文章主要讲:说epoll 有数据,会调用回调,将fd放到一个就绪队列。处理的不需要检查每个被监听的对象,只需要检查就绪队列的部分。select 和 poll就是都要检查,然后select 和poll 的话差别在与结构体有一定差别,没啥别的差别。具体细节不深究,用到再说。
https://www.cnblogs.com/Anker/p/3265058.html

这个文章,主要讲了下iO 的区分:可以了解下:
https://www.jianshu.com/p/8c368129c658
下面的文章,我认为讲的很清晰,比上面的清洗,上面主要还混了写netty的内容,所以保留:
https://www.jianshu.com/p/a6a38ed93fc2?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

相关文章

  • JavaNIO

    javaNIO整体上看到更多是API怎么用。buffer。channel。selector.更细的API用的时候了...

  • javaNIO

    一.NIO的由来 传统的BIO即阻塞IO,不管是磁盘IO还是网络IO,在写入和读取的时候,因为内存和硬盘或网络的读...

  • JavaNIO

    JavaNIO 一、IO与NIO IO是一次性操作一个字节的数据进行读写操作。我们称之为“面向流处理数据” NIO...

  • JavaNIO 下载网络文件保存本地报java.nio.file

    JavaNIO 下载文件保存本地报java.nio.file.AccessDeniedException:无权限操...

  • JavaNIO-Buffer

    Buffer java NIO库是在jdk1.4中引入的,NIO与IO之间的第一个区别在于,IO是面向流的,而NI...

  • JavaNIO-MappedByteBuffer

    内核空间与用户空间 Kernel space 是 Linux 内核的运行空间,User space 是用户程序的运...

  • JavaNIO-Channel

    Channel 通道(Channel)的作用有类似于流(Stream),用于传输文件或者网络上的数据。 上图中,箭...

  • JavaNIO源码分析

    首先给大家分享一个Linux下的OpenJDK1.8源码,这个源码里包含了sun包源码 ,自己要去找挺难找的,下面...

  • JavaNio-Selector

    一、概览 在这篇文章中,我们将探索一下JavaNIO的Selector组件。selector提供了一个机制,该机制...

  • Java NIO(1) 概述

    什么是JavaNIO NIO全名(NewInput/ Output),在java1.4中引入。NIO是一种同步...

网友评论

    本文标题:JavaNIO

    本文链接:https://www.haomeiwen.com/subject/zfpvjftx.html