Netty的socket编程(四)

2021-07-03 18:05

阅读:349

标签:put   bootstrap   net   handle   active   final   tin   rup   write   

Netty提供的handler:编解码,消息头,消息编码

 

1. 连接建立后,客户端和服务端都不会主动发消息,实现handlerchannelActive来触发消息发送。

2.SimpleChannelInboundHandler的重载方法

(1)handlerAdded  连接建立时调用

(2)HandlerRemoved 连接断开时调用

(3)HandlerActive 连接处于活动状态调用

(4)HandlerInactive 连接处于不活动状态调用

 

socket示例 :

server启动类:

 

 1 public class MyServer {
 2 
 3     public static void main(String[] args) throws InterruptedException {
 4 
 5         EventLoopGroup bossGroup = new NioEventLoopGroup();
 6         EventLoopGroup workerGroup = new NioEventLoopGroup();
 7 
 8         try{
 9             ServerBootstrap serverBootstrap = new ServerBootstrap();
10             serverBootstrap.group(bossGroup,workerGroup).
11                     channel(NioServerSocketChannel.class)
12                     .childHandler(new MyServerInitializer());
13             ChannelFuture channelFuture = serverBootstrap.bind(8899).sync();
14             channelFuture.channel().closeFuture().sync();
15 
16         }finally {
17 
18             bossGroup.shutdownGracefully();
19             workerGroup.shutdownGracefully();
20 
21         }
22 
23     }
24 
25 }

 

 

server初始化类 :

 1 public class MyServerInitializer  extends ChannelInitializer{
 2 
 3 
 4     @Override
 5     protected void initChannel(SocketChannel ch) throws Exception {
 6 
 7         ChannelPipeline channelPipeline = ch.pipeline();
 8 
 9         //解码器
10         channelPipeline.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE,0,4,0,4));
11         //消息 加header
12         channelPipeline.addLast(new LengthFieldPrepender(4));
13         channelPipeline.addLast(new StringDecoder(CharsetUtil.UTF_8));
14         channelPipeline.addLast(new StringEncoder(CharsetUtil.UTF_8));
15         channelPipeline.addLast(new MyServerHandler());
16 
17     }
18 }

 

server业务处理handler:

 1 public class MyServerHandler extends SimpleChannelInboundHandler {
 2     @Override
 3     protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
 4 
 5         System.out.println(ctx.channel().remoteAddress()+"msg:"+msg);
 6 
 7         ctx.channel().writeAndFlush("from server "+ UUID.randomUUID());
 8 
 9     }
10 
11     @Override
12     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
13         cause.printStackTrace();
14         ctx.close();
15     }
16 }

 

 

client 类

client启动类:

 1 public class MyClient {
 2 
 3     public static void main(String[] args) throws InterruptedException {
 4 
 5         EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
 6 
 7         try{
 8             Bootstrap bootstrap = new Bootstrap();
 9             bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class)
10                     .handler(new MyClientInitializer());
11 
12             ChannelFuture channelFuture = bootstrap.connect("localhost",8899).sync();
13             channelFuture.channel().closeFuture().sync();
14         }finally {
15             eventLoopGroup.shutdownGracefully();
16         }
17 
18 
19     }
20 
21 }

 

client初始化类 : 

 

 1 public class MyClientInitializer extends ChannelInitializer{
 2     @Override
 3     protected void initChannel(SocketChannel ch) throws Exception {
 4         ChannelPipeline channelPipeline = ch.pipeline();
 5         //解码器
 6         channelPipeline.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE,0,4,0,4));
 7         //消息 加header
 8         channelPipeline.addLast(new LengthFieldPrepender(4));
 9         channelPipeline.addLast(new StringDecoder(CharsetUtil.UTF_8));
10         channelPipeline.addLast(new StringEncoder(CharsetUtil.UTF_8));
11         channelPipeline.addLast(new MyClientHandler());  //自己的处理器
12     }
13 }

 

client业务处理handler :

 

 1 public class MyClientHandler extends SimpleChannelInboundHandler{
 2     @Override
 3     protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
 4         System.out.println(ctx.channel().remoteAddress());
 5         System.out.println("client output "+msg);
 6         ctx.writeAndFlush("from client "+ LocalDateTime.now());
 7 
 8     }
 9 
10     @Override
11     public void channelActive(ChannelHandlerContext ctx) throws Exception {
12         ctx.writeAndFlush("来自客户端的问候!!");
13     }
14 
15     @Override
16     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
17         cause.printStackTrace();
18         ctx.close();
19     }
20 }

 

Netty的socket编程(四)

标签:put   bootstrap   net   handle   active   final   tin   rup   write   

原文地址:http://www.cnblogs.com/fubinhnust/p/7123577.html


评论


亲,登录后才可以留言!