第11节网络编程快速入门案例

案例1NIO非阻塞网络编程快速入门

案例要求

编写一个NIO入门案例,实现服务端与客户端之间简单的数据通信

代码实现

服务端代码

public class Server {
    public static void main(String[] args) throws IOException {
        /*1.获取通道,创建NIO通道*/
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        /*2.切换为非阻塞模式*/
        serverSocketChannel.configureBlocking(false);
        /*3.绑定连接的端口*/
        serverSocketChannel.bind(new InetSocketAddress("localhost",9999));
        /*4.获取选择器selector*/
        Selector selector = Selector.open();
        /*5.将通道都注册到选择器上去,并且开始指定监听接收事件*/
        SelectionKey selectionKey1 = serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
        /*6.使用Selector选择器轮询已经准备好的事件*/
        while(selector.select()>0){
            /*7.获取选择器中的所有注册通道中已经就绪好的事件*/
            Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
            /*8.开始遍历这些已经准备好的事件*/
            while (iterator.hasNext()){
                /*9.获取当前的事件*/
                SelectionKey selectionKey = iterator.next();
                /*10.判断当前这个事件是什么,accept为建立连接的事件,需要将该事件注册到选择器中*/
                if (selectionKey.isAcceptable()){
                    /*11.直接获取当前接入的客户端通道*/
                    SocketChannel acceptChannel = serverSocketChannel.accept();
                    /*12.切换为非阻塞模式*/
                    acceptChannel.configureBlocking(false);
                    /*13.将本客户端通道注册到选择器*/
                    acceptChannel.register(selector,SelectionKey.OP_READ);
                }/*readable为读事件,说明当前有客户端发送过来消息*/
                else if (selectionKey.isReadable()){
                    /*14.获取当前选择器上的读就绪事件,根据selectionKey来获取对应的事件*/
                    SocketChannel channel = (SocketChannel) selectionKey.channel();
                    /*15.读取对应的事件*/
                    ByteBuffer buffer = ByteBuffer.allocate(1024);
                    while(channel.read(buffer)>0){
                        buffer.flip();
                        System.out.println(new String(buffer.array(),buffer.position(),buffer.remaining()));
                        /*清除之前的数据*/
                        buffer.clear();
                    }
                }
                /*该事件处理完毕移除该事件*/
              iterator.remove();
            }
        }
    }
}

客户端代码

public class Client {
    public static void main(String[] args) throws IOException {
        /*1.获取通道*/
        SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1", 9999));
        /*2.切换成非阻塞模式*/
        socketChannel.configureBlocking(false);
        /*3.分配指定缓冲区大小*/
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        /*4.发送数据给服务端*/
        Scanner scanner = new Scanner(System.in);
        while(true){
            System.out.println("客户端启动:");
            String msg = scanner.nextLine();
            buffer.put(("客户端1:"+msg).getBytes());
            buffer.flip();
            socketChannel.write(buffer);
            buffer.clear();
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值