2021-05-18

课堂代码

package ceshi;

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

/**
 * @author xcdq.chenlong
 * @date 2021/5/18 15:59
 */
public class SelectorServiceDemo {
    public static void main(String[] args) throws IOException {
        int prot = 800;

        //通过open()方法找到Selector
        Selector selector = Selector.open();

        //通过服务器的通道
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();

        //服务器配置为非阻塞
        serverSocketChannel.configureBlocking(false);
        ServerSocket serverSocket = serverSocketChannel.socket();
        InetSocketAddress address = new InetSocketAddress(prot);

        //进行服务的捆绑
        serverSocket.bind(address);
        //注册到selector,等待连接
        serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
        System.out.println("服务器运行,端口:" + prot);
        //数据缓冲区
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        while (true){
            if (selector.select() > 0){

                //选择以主键,并且相应的通道已经准备就绪
                // 取出全部生成的key
                Set selectionKeys = selector.selectedKeys();
                Iterator iter = selectionKeys.iterator();

                while (iter.hasNext()){

                    SelectionKey key = iter.next();
                    if (key.isAcceptable()){

                    }else if (key.isAcceptable() && key.isValid()){

                    }
                }


            }
        }


    }


}
package ceshi;

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.SocketChannel;
import java.sql.Connection;
import java.util.Iterator;
import java.util.Set;
import java.util.logging.SocketHandler;

/**
 * @author xcdq.chenlong
 * @date 2021/5/18 16:30
 */
public class SelectorSlientDemo {
    public static void main(String[] args) throws IOException {
        //打开socket通道
        SocketChannel socketChannel = SocketChannel.open();
        //设置为非阻塞方式
        socketChannel.configureBlocking(false);
        //通过open()方法动作找到
        Selector selector = Selector.open();

        //注册连接服务器socket动作
        socketChannel.register(selector, SelectionKey.OP_CONNECT);

        //连接
        socketChannel.connect(new InetSocketAddress("127.0.0.1",8000));
     //数据缓冲区
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        while (true){
            if (selector.select() > 0){
                //选择一主键,并且相应的通道已经准备就绪
                Set selectionKeys = selector.selectedKeys() ;  //取出全部生成的key
                Iterator iter = selectionKeys.iterator();
                while (iter.hasNext()){
                    SelectionKey key = iter.next();  //取出每一个key
                    if (key.isAcceptable()){

                    }
                }

            }

        }


    }



}

你可能感兴趣的:(2021-05-18)