nio 測試小代碼

package com.io;


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;

/**
 * \* Description:這裏是不允許 這個操作的。因爲SocketChannel 不允許有這個狀態
 * \* Created with IntelliJ IDEA.
 * \* User: fdes
 * \* Date: 2018/9/5
 * \* Time: 13:44
 * \
 */
public class ServerNio {

    private ServerSocketChannel ssc = null;
    private Selector selector = null;

    public static void main(String[] args) throws IOException {
        ServerNio ss = new ServerNio();
        ss.initServerChannel(8888);
        ss.linsternServerChannel();
    }

    private void initServerChannel(int port) throws IOException {
        ssc = ServerSocketChannel.open();
        ssc.configureBlocking(false);
        ssc.socket().bind(new InetSocketAddress(port));

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

    private void linsternServerChannel() throws IOException {
        while (true){
            this.selector.select();
            Iterator<SelectionKey>  iterator = this.selector.selectedKeys().iterator();
            while (iterator.hasNext()){
                SelectionKey key = iterator.next();
                iterator.remove();
                hanlder(key);
            }
        }

    }

    private void hanlder(SelectionKey key) throws IOException {

        if (key.isAcceptable()){
            ServerSocketChannel serverSocketChannel = (ServerSocketChannel)key.channel();
            SocketChannel sc = serverSocketChannel.accept();
            sc.configureBlocking(false);
            sc.register(this.selector,SelectionKey.OP_READ);
        } else if (key.isReadable()){
            SocketChannel sc = (SocketChannel)key.channel();
            ByteBuffer byteBuffers = ByteBuffer.allocate(1024);
            int read = sc.read(byteBuffers);
            if (read > 0){
                String command = new String(byteBuffers.array(),0,read,"utf-8");
                if (command.equals("re")){
                    System.out.println("=============re============"+command);
                    sc.register(this.selector,SelectionKey.OP_CONNECT);
                } else if (command.equals("wirte")){
                    System.out.println("==========wirte==============="+command);
                    sc.register(this.selector,SelectionKey.OP_WRITE);
                } else if (command.equals("error")){
                    System.out.println("==========error==============="+command);
                    //
                    sc.register(this.selector,SelectionKey.OP_ACCEPT);
                } else{
                    System.out.println("========================="+command);
                }
            } else {
                System.out.println("客戶端沒有數據......");
            }
        } else if (key.isWritable()){
            System.out.println("客戶端進行寫操作......");
            SocketChannel sc = (SocketChannel)key.channel();
            ByteBuffer byteBuffers = ByteBuffer.allocate(1024);
            byte[] bytes = "你好,客戶端1111111".getBytes();
            sc.write(byteBuffers.get(bytes));
            sc.register(this.selector,SelectionKey.OP_READ);
        }

    }

}

代碼做個筆記,方便以後複習:

1:SocketChannel (客戶端) 是不允許有 SelectionKey.OP_ACCEPT(接受就緒)狀態。

2:ServerSocketChannel  底層調用的還是ServerSocket   (ssc.socket() 返回的就是)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章