Run Windows environment, my code is as follows:
Private void the start () {
LogHelper. Info (" Start server... ");
System. Out.println (" Start server... ");
EventLoopGroup bossGroup=new NioEventLoopGroup ();
EventLoopGroup workerGroup=new NioEventLoopGroup ();
ServerBootstrap bootstrap=new ServerBootstrap ();
Try {
The bootstrap. Group (bossGroup workerGroup);
The bootstrap. Channel (NioServerSocketChannel. Class);
//final String port=ResourceUtil. GetKey (" local_tcp_port ");
//final String webPort=ResourceUtil. GetKey (" local_web_port ");
The bootstrap. ChildHandler (new ChannelInitializer
@ Override
Protected void initChannel Channel (Channel) {
Try {
//ByteBuf delimiter=Unpooled. CopiedBuffer (". "getBytes ());
//ByteBuf bb=Unpooled. Buffer (0, 2048);
Channel. The config (.) setRecvByteBufAllocator (new AdaptiveRecvByteBufAllocator (4, 2048, Integer. MAX_VALUE));
ChannelPipeline pipeline=channel. The pipeline ();
//IdleStateHandler heartbeat, if the timeout triggers userEventTrigger in the Handle () method
Pipeline. AddLast (new IdleStateHandler (15, 0, 0, TimeUnit. MINUTES));//heart 5 minutes
//pipeline. AddLast (new CustomDecoder (2048,0,0,0,0, false));
Pipeline. AddLast (new MyDecoder (2048));
////filter encoding
//pipeline. AddLast (" decoder, "new ByteArrayDecoder ());
////filter encoding
//pipeline. AddLast (" encoder ", new ByteArrayEncoder ());
InetSocketAddress inet=(InetSocketAddress) channel. LocalAddress ();
If (inet. GetPort ()==Integer. The parseInt (SMSTool. Local_tcp_port))
Pipeline. AddLast (new ServerHandler ());
//else if (inet. GetPort ()==Integer. The parseInt (SMSTool. Local_web_port))
//pipeline. AddLast (new WebServerHandler (initCallQueue));
} the catch (Exception e) {
//TODO: handle the exception
//LogHelper getIntance (DSD) equals (e. oString ());
LogHelper. Error (e. oString ());
}
}
}). Option (ChannelOption SO_BACKLOG, 1024)
//option (ChannelOption RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator (65535))
ChildOption (ChannelOption SO_KEEPALIVE, true);
//bind port, and returns the ChannelFuture
ChannelFuture future=the bootstrap. Bind (Integer. The parseInt (SMSTool. Local_tcp_port)). The sync ();
ChannelFuture future2=the bootstrap. Bind (Integer. The parseInt (SMSTool. Local_web_port)). The sync ();
System. Out.println (" server started!" );
LogHelper. Info (" server started!" );
GetConnectionTotal ();
//wait for the client closed
//Channel Channel=
Future. Channel (.) closeFuture (). The sync ();
Future2. Channel (.) closeFuture (). The sync ();
ResourceLeakDetector. SetLevel (ResourceLeakDetector. Level. The PARANOID);//memory leak detection development recommended the PARANOID online SIMPLE
//remember that closeFuture (), is used to waiting for the server shut down
//channel. CloseFuture (). The sync ();
} the catch (Exception e) {
e.printStackTrace();
} the finally {
BossGroup. ShutdownGracefully (.) syncUninterruptibly ();
WorkerGroup. ShutdownGracefully (.) syncUninterruptibly ();
}
}
The compiler code:
@ Override
Protected void decode (ChannelHandlerContext CTX, ByteBuf buffer, List