300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > Netty网络编程实战2 使用Netty开发聊天室功能

Netty网络编程实战2 使用Netty开发聊天室功能

时间:2021-07-17 10:11:23

相关推荐

Netty网络编程实战2 使用Netty开发聊天室功能

目录

一、服务端1、主程序类2、自定义初始化器3、自定义处理器 二、客户端1、主程序类2、自定义初始化器3、自定义处理器 三、启动服务端、客户端1、服务端:你好,我是服务端,哪吒编程2、客户端:我去,还真连上了,第一次使用Netty通话,真神奇3、服务端:土包子Java高并发编程实战系列文章哪吒精品系列文章

在 Netty网络编程实战1,搭建第一个Netty服务器中,使用curl作为客户端访问,下面将通过Netty实现客户端,客户端代码依然采用Netty老套路主程序类+自定义初始化器+自定义处理器三部分组成。

一、服务端

1、主程序类

package com.ty.chat;import ty.bootstrap.ServerBootstrap;import ty.channel.ChannelFuture;import ty.channel.EventLoopGroup;import ty.channel.nio.NioEventLoopGroup;import ty.channel.socket.nio.NioServerSocketChannel;public class MyNettyServerTest {public static void main(String[] args) throws InterruptedException {/*** EventLoopGroup:事件循环组,是一个线程池,也是一个死循环,用于不断地接收用户请求;* serverGroup:用户监听及建立连接,并把每一个连接抽象为一个channel,最后再将连接交给clientGroup处理;* clientGroup:真正的处理连接*/EventLoopGroup serverGroup = new NioEventLoopGroup();EventLoopGroup clientGroup = new NioEventLoopGroup();try {// 服务端启动时的初始化操作ServerBootstrap serverBootstrap = new ServerBootstrap();// 1、将serverGroup和clientGroup注册到服务端的Channel上;// 2、注册一个服务端的初始化器MyNettyServerInitializer;// 3、该初始化器中的initChannel()方法会在连接被注册到Channel后立刻执行;// 5、最后将端口号绑定到8080;ChannelFuture channelFuture = serverBootstrap.group(serverGroup, clientGroup).channel(NioServerSocketChannel.class).childHandler(new MyNettyServerInitializer()).bind(8080).sync();channelFuture.channel().closeFuture().sync();}catch (Exception e){System.out.println(e);}finally {serverGroup.shutdownGracefully();clientGroup.shutdownGracefully();}}}

2、自定义初始化器

package com.ty.chat;import ty.channel.ChannelInitializer;import ty.channel.ChannelPipeline;import ty.channel.socket.SocketChannel;import ty.handler.codec.LengthFieldBasedFrameDecoder;import ty.handler.codec.LengthFieldPrepender;import ty.handler.codec.string.StringDecoder;import ty.handler.codec.string.StringEncoder;import ty.util.CharsetUtil;/*** 自定义初始化器*/public class MyNettyServerInitializer extends ChannelInitializer<SocketChannel> {// 连接被注册到Channel后,立刻执行此方法@Overrideprotected void initChannel(SocketChannel socketChannel) throws Exception {ChannelPipeline pipeline = socketChannel.pipeline();/*** LengthFieldBasedFrameDecoder用于解析带固定长度的数据报。* TCP发送的数据规则:可以将数据进行拆分或合并,因此对端接收到的数据报可能不是初始发送时的格式;* 一般的做法是在包头设置length长度,指明数据包的长度,再由接受方根据length拼接或剪裁收到的数据,从而形成完整的数据包*/pipeline.addLast("LengthFieldBasedFrameDecoder",new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE,0,8,0,8));// 将上条语句的length加入传递的数据中心pipeline.addLast("LengthFieldPrepender",new LengthFieldPrepender(8));// 传递字符串的编码解码器pipeline.addLast("StringDecoder",new StringDecoder(CharsetUtil.UTF_8));pipeline.addLast("StringEecoder",new StringEncoder(CharsetUtil.UTF_8));// 增加自定义处理器MyNettyServerHandler,用于实际处理请求,并给出响应pipeline.addLast("MyNettyServerHandler",new MyNettyServerHandler());}}

3、自定义处理器

package com.ty.chat;import ty.channel.ChannelHandlerContext;import ty.channel.SimpleChannelInboundHandler;import java.util.Scanner;/*** 自定义处理器* Inbound代表"进入"的请求*/public class MyNettyServerHandler extends SimpleChannelInboundHandler<String> {@Overrideprotected void channelRead0(ChannelHandlerContext ctx, String receiveMsg) throws Exception {// 通过ctx获取客户端的IP和端口号,并打印出客户端发来的消息System.out.println("服务端接收的请求来自:"+ctx.channel().remoteAddress()+",消息内容:"+receiveMsg);System.out.println("请向客户端发送一条消息:");String sendMsg = new Scanner(System.in).nextLine();ctx.channel().writeAndFlush(sendMsg);}}

二、客户端

1、主程序类

package com.ty.chat;import ty.bootstrap.Bootstrap;import ty.channel.ChannelFuture;import ty.channel.EventLoopGroup;import ty.channel.nio.NioEventLoopGroup;import ty.channel.socket.nio.NioSocketChannel;/*** 主程序类*/public class MyNettyClientTest {public static void main(String[] args) {/*** 服务端有两个EventLoopGroup,serverGroup用于获取连接并将连接分发给clientGroup,clientGroup负责处理连接;* 对于客户端而言,客户端仅仅需要连接服务端即可*/EventLoopGroup eventLoopGroup = new NioEventLoopGroup();try {// 客户端启动时的初始化操作Bootstrap bootstrap = new Bootstrap();bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class).handler(new MyNettyClientInitializer());ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 8080).sync();channelFuture.channel().closeFuture().sync();}catch (Exception e){System.out.println(e);}finally {eventLoopGroup.shutdownGracefully();}}}

2、自定义初始化器

package com.ty.chat;import ty.channel.ChannelInitializer;import ty.channel.ChannelPipeline;import ty.channel.socket.SocketChannel;import ty.handler.codec.LengthFieldBasedFrameDecoder;import ty.handler.codec.LengthFieldPrepender;import ty.handler.codec.string.StringDecoder;import ty.handler.codec.string.StringEncoder;import ty.util.CharsetUtil;/*** 自定义初始化器*/public class MyNettyClientInitializer extends ChannelInitializer<SocketChannel> {// 连接被注册后,立即执行此方法@Overrideprotected void initChannel(SocketChannel socketChannel) throws Exception {ChannelPipeline pipeline = socketChannel.pipeline();/*** LengthFieldBasedFrameDecoder用于解析带固定长度的数据报。* TCP发送的数据规则:可以将数据进行拆分或合并,因此对端接收到的数据报可能不是初始发送时的格式;* 一般的做法是在包头设置length长度,指明数据包的长度,再由接受方根据length拼接或剪裁收到的数据,从而形成完整的数据包*/pipeline.addLast("LengthFieldBasedFrameDecoder",new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE,0,8,0,8));// 将上条语句的length加入传递的数据中心pipeline.addLast("LengthFieldPrepender",new LengthFieldPrepender(8));// 传递字符串的编码解码器pipeline.addLast("StringDecoder",new StringDecoder(CharsetUtil.UTF_8));pipeline.addLast("StringEecoder",new StringEncoder(CharsetUtil.UTF_8));// 增加自定义处理器MyNettyClientHandlerpipeline.addLast("MyNettyClientHandler",new MyNettyClientHandler());}}

3、自定义处理器

package com.ty.chat;import ty.channel.ChannelHandlerContext;import ty.channel.SimpleChannelInboundHandler;import java.util.Scanner;/*** 自定义处理器*/public class MyNettyClientHandler extends SimpleChannelInboundHandler<String> {@Overrideprotected void channelRead0(ChannelHandlerContext ctx, String receiveMsg) throws Exception {System.out.println("客户端接收的请求来自:"+ctx.channel().remoteAddress()+",消息内容:"+receiveMsg);System.out.println("请向服务端发送一条消息:");String sendMsg = new Scanner(System.in).nextLine();ctx.channel().writeAndFlush(sendMsg);}public void channelActive(ChannelHandlerContext ctx) throws Exception{ctx.writeAndFlush("第一条消息...");}}

三、启动服务端、客户端

1、服务端:你好,我是服务端,哪吒编程

2、客户端:我去,还真连上了,第一次使用Netty通话,真神奇

3、服务端:土包子

Java高并发编程实战系列文章

Java高并发编程实战1,那些年学过的锁

Java高并发编程实战2,原子性、可见性、有序性,傻傻分不清

Java高并发编程实战3,Java内存模型与Java对象结构

Java高并发编程实战4,synchronized与Lock底层原理

Java高并发编程实战5,异步注解@Async自定义线程池

哪吒精品系列文章

Java学习路线总结,搬砖工逆袭Java架构师

10万字208道Java经典面试题总结(附答案)

SQL性能优化的21个小技巧

Java基础教程系列

Spring Boot 进阶实战

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。