標籤:print throwable lease erb ssg auth nts 釋放 ref
服務端代碼:
package com.lsp.netty;/** * @author lishupeng * @create 2017-05-27 下午 3:48 **/import io.netty.bootstrap.ServerBootstrap;import io.netty.buffer.ByteBuf;import io.netty.buffer.Unpooled;import io.netty.channel.ChannelFuture;import io.netty.channel.ChannelInitializer;import io.netty.channel.ChannelOption;import io.netty.channel.EventLoopGroup;import io.netty.channel.nio.NioEventLoopGroup;import io.netty.channel.socket.SocketChannel;import io.netty.channel.socket.nio.NioServerSocketChannel;import io.netty.handler.codec.DelimiterBasedFrameDecoder;import io.netty.handler.codec.string.StringDecoder;public class Socket { public static void main(String[] args) throws InterruptedException { //1.第一個線程組是用於接收Client端串連的 EventLoopGroup bossGroup = new NioEventLoopGroup(); //2.第二個線程組是用於實際的業務處理的 EventLoopGroup workerGroup = new NioEventLoopGroup(); ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup);//綁定兩個線程池 b.channel(NioServerSocketChannel.class);//指定NIO的模式,如果是用戶端就是NioSocketChannel b.option(ChannelOption.SO_BACKLOG, 1024);//TCP的緩衝區設定 b.option(ChannelOption.SO_SNDBUF, 32*1024);//設定發送緩衝的大小 b.option(ChannelOption.SO_RCVBUF, 32*1024);//設定接收緩衝區大小 b.option(ChannelOption.SO_KEEPALIVE, true);//保持連續 b.childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel sc) throws Exception { ByteBuf buf = Unpooled.copiedBuffer("$_".getBytes());//拆包粘包定義結束字串(第一種解決方案) sc.pipeline().addLast(new DelimiterBasedFrameDecoder(1024,buf));//在管道中加入結束字串 // sc.pipeline().addLast(new FixedLengthFrameDecoder(200));第二種定長 sc.pipeline().addLast(new StringDecoder());//定義接收類型為字串把ByteBuf轉成String sc.pipeline().addLast(new ServertHandler());//在這裡配置具體資料接收方法的處理 } }); ChannelFuture future = b.bind(8765).sync();//綁定連接埠 future.channel().closeFuture().sync();//等待關閉(程式阻塞在這裡等待用戶端請求) bossGroup.shutdownGracefully();//關閉線程 workerGroup.shutdownGracefully();//關閉線程 }}
package com.lsp.netty;import io.netty.buffer.Unpooled;import io.netty.channel.ChannelHandlerAdapter;import io.netty.channel.ChannelHandlerContext;/** * @author lishupeng * @create 2017-05-27 下午 3:48 **/public class ServertHandler extends ChannelHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { String body = (String) msg; System.out.println("server"+body);//前面已經定義了接收為字串,這裡直接接收字串就可以 //服務端給用戶端的響應 String response= " hi client!$_";//發送的資料以定義結束的字串結尾 ctx.writeAndFlush(Unpooled.copiedBuffer(response.getBytes()));//發送必須還是ByteBuf類型 } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { cause.printStackTrace(); ctx.close(); }}
用戶端代碼:
package com.lsp.netty;import io.netty.bootstrap.Bootstrap;import io.netty.buffer.ByteBuf;import io.netty.buffer.Unpooled;import io.netty.channel.ChannelFuture;import io.netty.channel.ChannelInitializer;import io.netty.channel.EventLoopGroup;import io.netty.channel.nio.NioEventLoopGroup;import io.netty.channel.socket.SocketChannel;import io.netty.channel.socket.nio.NioSocketChannel;import io.netty.handler.codec.DelimiterBasedFrameDecoder;import io.netty.handler.codec.string.StringDecoder;/** * @author lishupeng * @create 2017-05-27 下午 3:48 **/public class Client { public static void main(String[] args) throws InterruptedException { EventLoopGroup worker = new NioEventLoopGroup(); Bootstrap b = new Bootstrap(); b.group(worker) .channel(NioSocketChannel.class) .handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel sc) throws Exception { ByteBuf buf = Unpooled.copiedBuffer("$_".getBytes()); sc.pipeline().addLast(new DelimiterBasedFrameDecoder(1024,buf)); sc.pipeline().addLast(new StringDecoder()); sc.pipeline().addLast(new ClientHandler()); } }); ChannelFuture f=b.connect("127.0.0.1",8765).sync(); f.channel().writeAndFlush(Unpooled.copiedBuffer(" hi server2$_".getBytes())); f.channel().writeAndFlush(Unpooled.copiedBuffer(" hi server3$_".getBytes())); f.channel().writeAndFlush(Unpooled.copiedBuffer(" hi server4$_".getBytes())); f.channel().closeFuture().sync(); worker.shutdownGracefully(); }}
package com.lsp.netty;import io.netty.channel.ChannelHandlerAdapter;import io.netty.channel.ChannelHandlerContext;import io.netty.util.ReferenceCountUtil;/** * @author lishupeng * @create 2017-05-27 下午 3:49 **/public class ClientHandler extends ChannelHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { try { System.out.println("client"+msg.toString()); } finally { ReferenceCountUtil.release(msg);//釋放緩衝區 } } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { cause.printStackTrace(); ctx.close(); }}
netty入門代碼學習