zoukankan      html  css  js  c++  java
  • 利用netty简单实现聊天室

    1.导入依赖包

            <dependency>
                <groupId>io.netty</groupId>
                <artifactId>netty-all</artifactId>
                <version>5.0.0.Alpha1</version>
            </dependency>

    2.netty服务端代码

    复制代码
    public class NettyServer {
        public static void main(String[] args) {
             EventLoopGroup bossGroup = new NioEventLoopGroup();
             EventLoopGroup workGroup = new NioEventLoopGroup();
             ServerBootstrap bootStrap = new ServerBootstrap();
                ChannelFuture cf;
                bootStrap.group(bossGroup,workGroup)
                        .channel(NioServerSocketChannel.class)
                        .option(ChannelOption.SO_BACKLOG, 1024)
                        .childHandler(new ChannelInitializer<SocketChannel>() {
                            @Override
                            public void initChannel(SocketChannel ch) throws Exception {
                                ChannelPipeline p = ch.pipeline();
                                p.addLast("decoder", new StringDecoder());//需要编解码,否则无法解析
                                p.addLast("encoder", new StringEncoder());
                                p.addLast(new NettyServerHandler());
                            }
                        });
    
                try {
                    cf =  bootStrap.bind(8099).sync();//监听8099端口
                    System.out.println("8099:binded...");
                    cf.channel().closeFuture().sync();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally{
                    bossGroup.shutdownGracefully();
                    workGroup.shutdownGracefully();
                }
        }
    }
    复制代码

    3.netty客户端代码

    复制代码
    public class NettyClient {
        public static void main(String[] args) throws Exception {
            EventLoopGroup group =new NioEventLoopGroup();
            try {
                Bootstrap b = new Bootstrap();
                b.group(group)
                        .channel(NioSocketChannel.class)
                        .option(ChannelOption.TCP_NODELAY, true)
                        .handler(new ChannelInitializer<SocketChannel>(){
                            @Override
                            public void initChannel(SocketChannel ch) throws Exception {
                                ChannelPipeline p = ch.pipeline();
                                p.addLast("decoder", new StringDecoder());
                                p.addLast("encoder", new StringEncoder());
                                p.addLast(new ClientHandler());
                                p.addLast(new ClientHandlerBak());
                            }
                        });
    
                ChannelFuture future = b.connect("127.0.0.1", 8099).sync();
                future.channel().writeAndFlush("这里是客户端,请求连接服务端!");
                future.channel().closeFuture().sync();
            } finally {
                group.shutdownGracefully();
            }
    
        }
    }
    复制代码

    4.服务端处理类

    复制代码
    public class NettyServerHandler extends ChannelHandlerAdapter {
    
        //有客户端连接时触发
        @Override
        public void channelActive(ChannelHandlerContext ctx) throws Exception {
            System.out.println("one client connect...");
        }
    
        //断开连接时触发
        @Override
        public void channelInactive(ChannelHandlerContext ctx) throws Exception {
            System.out.println("one client disconnect...");
        }
    
        //接收客户端发送的消息
        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            System.out.println("客户端:"+msg.toString());
            InputStreamReader is = new InputStreamReader(System.in);
            BufferedReader br = new BufferedReader(is);
            String result ="result";
            try{
                result = br.readLine();
            }
            catch(IOException e){
                e.printStackTrace();
            }
            ctx.write(result);//给客户端回复
            ctx.flush();
        }
    }
    复制代码

    5.客户端处理类

    复制代码
    public class ClientHandler extends ChannelHandlerAdapter {
    
        @Override
        public void channelActive(ChannelHandlerContext ctx) throws Exception {
            System.out.println("Client01Handler Active");
            /*若把这一句注释掉将无法将event传递给下一个ClientHandler,例如例子中p.addLast(new Client01Handler())后面紧跟着p.addLast(new Client02Handler())
             后面的Client02Handler里的方法就不会被触发。
            */
            ctx.fireChannelActive();
        }
    
        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            System.out.println("服务端: "+msg);
            InputStreamReader is = new InputStreamReader(System.in);
            BufferedReader br = new BufferedReader(is);
            String result ="result";
            try{
                result = br.readLine();
            }
            catch(IOException e){
                e.printStackTrace();
            }
            ctx.write(result);//给服务端回复
            ctx.flush();
        }
    
        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            cause.printStackTrace();
            ctx.close();
        }
    }
    复制代码

    6.最后,看看效果,先启动服务端,再启动客户端

  • 相关阅读:
    moviepy音视频剪辑VideoClip类fl_image方法image_func报错ValueError: assignment destination is read-only解决办法
    moviepy音视频剪辑VideoClip类set_position方法pos参数的使用方法及作用
    moviepy音视频剪辑VideoClip类to_ImageClip方法使用注意事项
    moviepy音视频剪辑VideoClip类to_mask方法、to_RGB、afx方法
    moviepy音视频剪辑:视频剪辑基类VideoClip的属性及方法详解
    moviepy音视频剪辑:视频剪辑基类VideoClip详解
    老猿学5G:融合计费的Nchf和Nchf‘服务化接口消息Nchf_ConvergedCharging_Create、Update、Release和Notify
    Loadrunner 11 中Run-Time Setting详细参数说明
    Sublime 3基于python环境的使用
    LoadRunner常用方法
  • 原文地址:https://www.cnblogs.com/tinyj/p/10122952.html
Copyright © 2011-2022 走看看