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<SelectionKey> selectionKeys = selector.selectedKeys();
                Iterator<SelectionKey> 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<SelectionKey> selectionKeys = selector.selectedKeys() ;  //取出全部生成的key
                Iterator<SelectionKey> iter = selectionKeys.iterator();
                while (iter.hasNext()){
                    SelectionKey key = iter.next();  //取出每一个key
                    if (key.isAcceptable()){

                    }
                }

            }

        }


    }



}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 看一篇文章,写的是内卷。我看到题目的时候,心想什么是内卷。看完后,觉得有一些到了,文章涉及一些表面工程,但我更看重...
    浅浅慢慢阅读 1,387评论 1 1
  • 《不同作家笔下的守财奴》 因为作家不同,所以他们笔下的守财奴形象也会不同。除了有共同的特征之外,也有他们个...
    橘子乐了阅读 3,401评论 0 0
  • 近来,好像诸事不顺,而仔细想想又觉得理应如此。 上学期期末考试以后,日子每天都过得中规中矩,结合成...
    85c03a242a07阅读 1,094评论 0 0
  • 关于课堂教学管理,在听课的过程中,忽然想到我们倡导以学生为主体,用演戏来讲,就是主角,我想没有一部剧作不会让主角的...
    浅浅慢慢阅读 1,225评论 0 0
  • 我是黑夜里大雨纷飞的人啊 1 “又到一年六月,有人笑有人哭,有人欢乐有人忧愁,有人惊喜有人失落,有的觉得收获满满有...
    陌忘宇阅读 12,755评论 28 53

友情链接更多精彩内容