自定義分割符,用:DelimiterBasedFrameDecoder類ide
ByteBuf轉String,用StringDecoder類oop
參考代碼:線程
//設置鏈接符/分隔符,換行顯示 ByteBuf buf = Unpooled.copiedBuffer("$_".getBytes()); //DelimiterBasedFrameDecoder:自定義分隔符 sc.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, buf)); //設置爲字符串形式的解碼:將傳遞的buf改成String sc.pipeline().addLast(new StringDecoder()); //處理數據 sc.pipeline().addLast(new ClientHandler());
完整代碼:code
client代碼server
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 { // TODO Auto-generated method stub //設置鏈接符/分隔符,換行顯示 ByteBuf buf = Unpooled.copiedBuffer("$_".getBytes()); //DelimiterBasedFrameDecoder:自定義分隔符 sc.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, buf)); //設置爲字符串形式的解碼:將傳遞的buf改成String sc.pipeline().addLast(new StringDecoder()); sc.pipeline().addLast(new ClientHandler()); } }); //鏈接端口 ChannelFuture cf = b.connect("127.0.0.1", 8765).sync(); cf.channel().writeAndFlush(Unpooled.copiedBuffer("aaa$_".getBytes())); cf.channel().writeAndFlush(Unpooled.copiedBuffer("bbbbb$_".getBytes())); cf.channel().writeAndFlush(Unpooled.copiedBuffer("cccccccc$_".getBytes())); cf.channel().closeFuture().sync(); worker.shutdownGracefully(); }
clientHandler代碼blog
public class ClientHandler extends ChannelHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { // TODO Auto-generated method stub //super.channelRead(ctx, msg); try { //在傳輸的時候已經將ByteBuf轉爲string String str = (String)msg; System.out.println("Client: " + str); } finally { // TODO: handle finally clause ReferenceCountUtil.release(msg); } } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { // TODO Auto-generated method stub cause.printStackTrace(); ctx.close(); } }
Server代碼:接口
public static void main(String[] args) throws InterruptedException { //待client鏈接的線程 EventLoopGroup boss = new NioEventLoopGroup(); //處理事務的線程 EventLoopGroup worker = new NioEventLoopGroup(); //bootstarp輔助類,註冊server服務 ServerBootstrap b = new ServerBootstrap(); b.group(boss, worker) .channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 1024) .option(ChannelOption.SO_SNDBUF, 32*1024) .option(ChannelOption.SO_RCVBUF, 32*1024) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel sc) throws Exception { // TODO Auto-generated method stub //設置鏈接符,換行顯示 ByteBuf buf = Unpooled.copiedBuffer("$_".getBytes()); //DelimiterBasedFrameDecoder:自定義分隔符 sc.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, buf)); //將buf轉string sc.pipeline().addLast(new StringDecoder()); sc.pipeline().addLast(new ServerHandler()); } }); //指定監聽接口 ChannelFuture cf = b.bind(8765).sync(); cf.channel().closeFuture().sync(); boss.shutdownGracefully(); worker.shutdownGracefully(); }
ServerHandler代碼事務
public class ServerHandler extends ChannelHandlerAdapter{ @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { // TODO Auto-generated method stub //super.channelRead(ctx, msg); //handler設置了buf轉String String str = (String)msg; System.out.println("Serer:" + str); String response = "我是響應的數據$_"; ctx.writeAndFlush(Unpooled.copiedBuffer(response.getBytes())); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { // TODO Auto-generated method stub //super.exceptionCaught(ctx, cause); cause.printStackTrace(); ctx.close(); } }