网络编程 -- RPC实现原理 -- Netty -- 迭代版本V1 -- 入门应用
2023-09-11 14:14:57 时间
啦啦啦
V1——Netty入门应用
Class : NIOServerBootStrap
package lime.pri.limeNio.netty.netty01.server; import java.net.InetSocketAddress; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel; public class NIOServerBootStrap { public static void main(String[] args) throws InterruptedException { ServerBootstrap bootstrap = new ServerBootstrap(); EventLoopGroup boss = new NioEventLoopGroup(); EventLoopGroup worker = new NioEventLoopGroup(); bootstrap.group(boss, worker); bootstrap.channel(NioServerSocketChannel.class); bootstrap.childHandler(new CustomServerChannelInitializer()); ChannelFuture channelFuture = bootstrap.bind(new InetSocketAddress(9999)).sync(); channelFuture.channel().closeFuture().sync(); boss.shutdownGracefully(); worker.shutdownGracefully(); } }
Class : CustomServerChannelInitializer
package lime.pri.limeNio.netty.netty01.server; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelPipeline; import io.netty.channel.socket.SocketChannel; public class CustomServerChannelInitializer extends ChannelInitializer<SocketChannel>{ @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast(new CustomServerChannelHandlerAdapter()); } }
Class : CustomServerChannelHandlerAdapter
package lime.pri.limeNio.netty.netty01.server; import java.util.Date; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; import io.netty.util.CharsetUtil; public class CustomServerChannelHandlerAdapter extends ChannelHandlerAdapter { /** * 服务器和客户端会话异常 */ @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { // TODO Auto-generated method stub super.exceptionCaught(ctx, cause); } /** * 当服务器与客户端联通时,通道被激活。 */ @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { // TODO Auto-generated method stub super.channelActive(ctx); } /** * 当服务器与客户端断开时,该方法被调用。 */ @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { // TODO Auto-generated method stub super.channelInactive(ctx); } /** * 通道读操作就绪,读取客户端消息 */ @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { ByteBuf buffer = (ByteBuf)msg; String request = buffer.toString(CharsetUtil.UTF_8); System.out.println("客户端请求数据:" + request); buffer.clear(); buffer.writeBytes(new Date().toString().getBytes()); ChannelFuture channelFuture = ctx.writeAndFlush(buffer); channelFuture.addListener(ChannelFutureListener.CLOSE); } }
Class :
package lime.pri.limeNio.netty.netty01.client; import java.io.IOException; import java.net.InetSocketAddress; import io.netty.bootstrap.Bootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioSocketChannel; public class NIOBootStrap { public static void main(String[] args) throws IOException, InterruptedException { Bootstrap bootstrap = new Bootstrap(); EventLoopGroup worker = new NioEventLoopGroup(); bootstrap.group(worker); bootstrap.channel(NioSocketChannel.class); bootstrap.handler(new CustomClientChannelInitializer()); ChannelFuture channelFuture = bootstrap.connect(new InetSocketAddress("127.0.0.1", 9999)).sync(); channelFuture.channel().closeFuture().sync(); worker.shutdownGracefully(); } }
Class : CustomClientChannelInitializer
package lime.pri.limeNio.netty.netty01.client; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelPipeline; import io.netty.channel.socket.SocketChannel; public class CustomClientChannelInitializer extends ChannelInitializer<SocketChannel>{ @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast(new CustomClientChannelHandlerAdapter()); } }
Class : CustomClientChannelHandlerAdapter
package lime.pri.limeNio.netty.netty01.client; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; import io.netty.util.CharsetUtil; public class CustomClientChannelHandlerAdapter extends ChannelHandlerAdapter { /** * 服务器和客户端会话异常 */ @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { // TODO Auto-generated method stub super.exceptionCaught(ctx, cause); } /** * 当服务器与客户端联通时,通道被激活。 */ @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { ByteBuf buffer = Unpooled.buffer(); ctx.writeAndFlush(buffer.writeBytes("Query Date".getBytes())); } /** * 当服务器与客户端断开时,该方法被调用。 */ @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { // TODO Auto-generated method stub super.channelInactive(ctx); } /** * 通道多操作就绪,读取服务端消息 */ @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { System.out.println("服务端响应数据:" + ((ByteBuf) msg).toString(CharsetUtil.UTF_8)); // 会话关闭操作由服务端启动,客户端不主动关闭会话。 } }
Console : Server
客户端请求数据:Query Date
Console : Client
服务端响应数据:Sat Jun 24 17:43:41 CST 2017
啦啦啦
相关文章
- 《 线性代数及其应用 (原书第4版)》—— 2.2 矩阵的逆
- axios网络交互应用-Vue
- ASP.NET Core中Ocelot的使用:API网关的应用
- 【快应用】视频如何根据网络状态播放和暂停
- 集成机器学习服务上架华为应用市场指南
- 《开源硬件创客——15个酷应用玩转树莓派》——第1章 初识树莓派
- SuperSocket性能数据采集的应用程序接口的改动
- Windows上安装配置SSH教程(8)——综合应用:在Windows上使用手动方式实现SSH远程登陆与文件传输
- SwiftUI Django之 03 编写第一个Django应用
- 《黑客大曝光:移动应用安全揭秘及防护措施》一2.1 基础移动网络功能
- C语言学习及应用笔记之一:C运算符优先级及使用问题
- 网络提速助推智能家居 智慧应用惠及千家
- 【快应用】车机快应用如何监听网络变化?
- 硬件商抢物联网“头啖汤” 价值链渐向应用平台拓展
- Spark应用远程调试
- 关于栈及其应用演示样例
- 关系网络在信贷领域的应用(转)
- swift网络编程入门应用:天气预报
- scapy在wlan中的应用