1. 程式人生 > >Netty的socket編程(四)

Netty的socket編程(四)

value oot pipeline rep rgs from msg oss 服務端

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<SocketChannel>{
 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<String> {
 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<SocketChannel>{
 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<String>{
 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編程(四)