netty 瑙e喅TCP绮樺寘涓庢媶鍖呴棶棰?涓?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了netty 瑙e喅TCP绮樺寘涓庢媶鍖呴棶棰?涓?相关的知识,希望对你有一定的参考价值。
鏍囩锛?/p>
浠婂ぉ浣跨敤netty鐨勫浐瀹氶暱搴﹁繘琛岃В鐮?/span>
鍥哄畾闀垮害瑙g爜鐨勫師鐞嗗氨鏄寜鐓ф寚瀹氭秷鎭殑闀垮害瀵规秷鎭嚜鍔ㄨВ鐮併€?/span>
鍦╪etty瀹炵幇涓紝鍙渶瑕侀噰鐢‵iexedLengthFrameDecoder瑙g爜鍣ㄥ嵆鍙?..
浠ヤ笅鏄湇鍔$浠g爜
1 package com.ming.netty.nio.stickpack; 2 3 4 5 import java.net.InetSocketAddress; 6 7 import io.netty.bootstrap.ServerBootstrap; 8 import io.netty.channel.ChannelFuture; 9 import io.netty.channel.ChannelInitializer; 10 import io.netty.channel.ChannelOption; 11 import io.netty.channel.EventLoopGroup; 12 import io.netty.channel.nio.NioEventLoopGroup; 13 import io.netty.channel.socket.SocketChannel; 14 import io.netty.channel.socket.nio.NioserverSocketChannel; 15 import io.netty.handler.codec.FixedLengthFrameDecoder; 16 import io.netty.handler.codec.string.StringDecoder; 17 import io.netty.handler.logging.LogLevel; 18 import io.netty.handler.logging.LoggingHandler; 19 20 public class EchoServer { 21 22 public void bind(String addr,int port) throws Exception{ 23 EventLoopGroup bossGroup=new NioEventLoopGroup(); 24 EventLoopGroup workGroup=new NioEventLoopGroup(); 25 try { 26 ServerBootstrap server=new ServerBootstrap(); 27 server.group(bossGroup,workGroup) 28 .channel(NioServerSocketChannel.class) 29 .option(ChannelOption.SO_BACKLOG, 100) 30 .handler(new LoggingHandler(LogLevel.INFO)) 31 .childHandler(new ChannelInitializer<SocketChannel>() { 32 33 @Override 34 protected void initChannel(SocketChannel sc) throws Exception { 35 sc.pipeline().addLast(new FixedLengthFrameDecoder(50));//FixedLengthFrameDecoder 杩欎釜绫诲氨鏄浐瀹氶暱搴﹁В鐮佸櫒锛屾剰鎬濆氨鏄皢娑堟伅鎸囧畾闀垮害杩涜瑙g爜 36 sc.pipeline().addLast(new StringDecoder()); 37 sc.pipeline().addLast(new EchoServerHandler()); 38 39 } 40 41 }); 42 ChannelFuture f=server.bind(new InetSocketAddress(addr, port)).sync(); 43 System.out.println("鍚姩鏈嶅姟鍣?"+f.channel().localAddress()); 44 //绛夌瓑鏈嶅姟鍣ㄧ鐩戝惉绔彛鍏抽棴 45 f.channel().closeFuture().sync(); 46 } catch (Exception e) { 47 e.printStackTrace(); 48 }finally{ 49 bossGroup.shutdownGracefully(); 50 workGroup.shutdownGracefully(); 51 } 52 } 53 54 55 public static void main(String[] args) throws Exception{ 56 new EchoServer().bind("192.168.1.108", 8500); 57 } 58 59 }
1 package com.ming.netty.nio.stickpack; 2 3 import io.netty.buffer.ByteBuf; 4 import io.netty.buffer.Unpooled; 5 import io.netty.channel.ChannelHandlerAdapter; 6 import io.netty.channel.ChannelHandlerContext; 7 8 public class EchoServerHandler extends ChannelHandlerAdapter{ 9 10 int count=0; 11 12 @Override 13 public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { 14 15 String body=(String)msg; 16 System.out.println("鏈嶅姟鍣ㄦ敹鍒?+(++count)+"娆″鎴风娑堟伅锛屾秷鎭槸:"+body); 17 body+="$_"; 18 ByteBuf rep=Unpooled.copiedBuffer(body.getBytes()); 19 ctx.writeAndFlush(rep); 20 } 21 22 @Override 23 public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { 24 cause.printStackTrace(); 25 ctx.close(); 26 } 27 28 29 }
娉ㄦ剰: FixedLengthFrameDecoder 杩欎釜绫昏В鐮佸悗锛屼綘瀹㈡埛绔瘡娆″氨浼氭寜鐓ф寚瀹氶暱搴﹁繘琛屾暟鎹帴鏀朵簡锛屽搱鍝?..渚嬪瓙涓槸50涓暱搴︾殑瀛楃鍝?..
澶╁ぉ瀛︿範锛屽ぉ澶╄繘姝ャ€傘€傘€傘€傘€傘€?/p>
瑙夊緱涓嶉敊鐐逛釜璧炲惂...
以上是关于netty 瑙e喅TCP绮樺寘涓庢媶鍖呴棶棰?涓?的主要内容,如果未能解决你的问题,请参考以下文章
Linux mysql蹇樿瀵嗙爜 淇敼瀵嗙爜 瑙e喅鏂规硶
瑙e喅java web 椤圭洰鍙戝竷webservice鎺ュ彛