当前位置: 首页> 游戏> 手游 > 哈尔滨seo优化排名免费咨询_著名品牌营销策划公司_湖南seo服务_深圳百度推广seo公司

哈尔滨seo优化排名免费咨询_著名品牌营销策划公司_湖南seo服务_深圳百度推广seo公司

时间:2025/7/9 8:20:51来源:https://blog.csdn.net/qq_28911061/article/details/146012834 浏览次数:0次
哈尔滨seo优化排名免费咨询_著名品牌营销策划公司_湖南seo服务_深圳百度推广seo公司

Netty笔记1:线程模型

Netty笔记2:零拷贝

Netty笔记3:NIO编程

Netty笔记4:Epoll

Netty笔记5:Netty开发实例

Netty笔记6:Netty组件

Netty笔记7:ChannelPromise通知处理

Netty笔记8:ByteBuf使用介绍

Netty笔记9:粘包半包

Netty笔记10:LengthFieldBasedFrameDecoder

Netty笔记11:编解码器

Netty笔记12:模拟Web服务器

Netty笔记13:序列化

文章目录

  • 示例
  • 总结

示例

服务端处理器

public class ServerHandler extends ChannelInboundHandlerAdapter {@Overridepublic void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {ByteBuf buffer = (ByteBuf)msg;System.out.println("接收到数据:" + buffer.toString(CharsetUtil.UTF_8));super.channelRead(ctx, msg);}@Overridepublic void channelActive(ChannelHandlerContext ctx) throws Exception {ctx.writeAndFlush(Unpooled.copiedBuffer(("服务器已接受。"+System.getProperty("line.separator")).getBytes(CharsetUtil.UTF_8)));}
}

服务端

public class NettyServer {/*创建这个服务端,和NIO编程有很多相像之处,*/public static void main(String[] args) throws InterruptedException {// 线程组NioEventLoopGroup group = new NioEventLoopGroup();// 启动器ServerBootstrap bootstrap = new ServerBootstrap();// 添加线程组try {bootstrap.group(group)// server指定为ServerChannel.channel(NioServerSocketChannel.class)// 绑定端口.localAddress(8080)// 事件处理器// 通过channel初始化器添加handle// ServerSocketChannel要产生socketChannel,便要对socketChannel进行初始化// 因为childHandler是对应socket,这里是server,会有很多客户端的socket.childHandler(new ChannelInitializer<SocketChannel>() {@Overrideprotected void initChannel(SocketChannel socketChannel) throws Exception {// handle是在piple中执行的,所以这里要添加到piplesocketChannel.pipeline().addLast(new ServerHandler());}});// 绑定到服务器(这里是异步的)ChannelFuture future = bootstrap.bind()// 这里阻塞直到绑定完成.sync();// 同样,阻塞直到channel关闭future.channel().closeFuture().sync();} catch (InterruptedException e) {group.shutdownGracefully().sync();}}
}

客户端处理器

public class ChildHandler extends SimpleChannelInboundHandler<ByteBuf> {@Overrideprotected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) throws Exception {System.out.println("客户端收到:" + byteBuf.toString(CharsetUtil.UTF_8));channelHandlerContext.channel();}@Overridepublic void channelActive(ChannelHandlerContext ctx) throws Exception {// 连接成功后激活这个方法ctx.writeAndFlush(Unpooled.copiedBuffer("你好".getBytes(CharsetUtil.UTF_8)));}
}

客户端

public class NettyClient {public static void main(String[] args) throws InterruptedException {// 线程组NioEventLoopGroup group = new NioEventLoopGroup();// 启动器Bootstrap bootstrap = new Bootstrap();try {// 设置线程组bootstrap.group(group)// 治党channel.channel(NioSocketChannel.class)// 指定服务端地址.remoteAddress(new InetSocketAddress("127.0.0.1", 8080))// 客户端都是socketChannel.handler(new ChannelInitializer<SocketChannel>() {@Overrideprotected void initChannel(SocketChannel socketChannel) throws Exception {// 只要是netty,那么都需要添加到piple// 处理器都是在piple中执行socketChannel.pipeline().addLast(new ChildHandler());}});// 阻塞直到连接完成;// sync()是阻塞ChannelFuture future = bootstrap.connect().sync();//future.channel().closeFuture().sync();} catch (InterruptedException e) {group.shutdownGracefully().sync();}}
}

image-20250114182339998image-20250114182356981

总结

Netty的编程可以说是比较简单的,他是一个框架,只需要固定的模式去编写就行,如上面示例中NioEventLopGropBootstrap,每个Netty编程都需要写,是一个固定句式,还有后面的channel、address、handler都是固定句式,所以,我们只要按照规定句式编写,就可以完成一个简单的Netty应用程序,而业务逻辑处理,通过Handler添加就行。

关键字:哈尔滨seo优化排名免费咨询_著名品牌营销策划公司_湖南seo服务_深圳百度推广seo公司

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: