NIO 代码demo

NIO 代码demo

1.服务端代码

/**
 * @author : zjx
 * @Description :
 * @date : Created in 15:03 2021/5/7
 * @Modified By:
 */
public class NioServer {

    public static void main(String[] args) throws IOException {
        ServerSocketChannel serverSocketChannel=ServerSocketChannel.open();
        //配置为非阻塞
        serverSocketChannel.configureBlocking(false);
        //绑定端口
        serverSocketChannel.bind(new InetSocketAddress("127.0.0.1",6666));
        //selector
        Selector selector = Selector.open();
        //serverSocketChannel 注册到selector中,并且监听连接时间
        serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
        while (true){
            if(selector.select(1000)==0){
                System.out.println("未检测出的连接");
                continue;
            }
            //所有发生事件的通道 对应的SelectionKey
            Set<SelectionKey> selectionKeys = selector.selectedKeys();
            Iterator<SelectionKey> iterator = selectionKeys.iterator();
            while (iterator.hasNext()) {
                SelectionKey selectionKey = iterator.next();
                if (selectionKey.isAcceptable()) {
                    //获取新连接的客户端Socket
                    SocketChannel socketChannel = serverSocketChannel.accept();
                    //配置为非阻塞
                    socketChannel.configureBlocking(false);
                    //把客户端Socket 注册进selector 并且监听 读 事件,以及配置服务端缓存区
                    socketChannel.register(selector,SelectionKey.OP_READ, ByteBuffer.allocate(512));
                }else if(selectionKey.isReadable()){
                    //获取发生的读事件的客户端Socket
                    SocketChannel socketChannel = (SocketChannel) selectionKey.channel();
                    //获取缓存区
                    ByteBuffer buffer = (ByteBuffer) selectionKey.attachment();
                    buffer.clear();
                    socketChannel.read(buffer);
                    System.out.println(new String(buffer.array()));
                }
                //移除通道,避免重复处理
                iterator.remove();
            }
        }
    }
}

2.客户端代码

/**
 * @author : zjx
 * @Description :
 * @date : Created in 15:25 2021/5/7
 * @Modified By:
 */
public class NioClient {
    public static void main(String[] args) throws IOException {
        SocketChannel socketChannel=SocketChannel.open();
        socketChannel.configureBlocking(false);
        InetSocketAddress address = new InetSocketAddress("127.0.0.1", 6666);
        if (!socketChannel.connect(address)) {
            while (!socketChannel.finishConnect()){
                System.out.println("连接中,客户端可以进行其他工作");
            }
            String str="hello world!";
            ByteBuffer wrap = ByteBuffer.wrap(str.getBytes());
            socketChannel.write(wrap);
            //避免客户端中断
            System.in.read();
        }

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值