netty simple demo

来源:互联网 发布:电脑控制多个手机源码 编辑:程序博客网 时间:2024/06/06 13:21
package com.netty;import io.netty.bootstrap.ServerBootstrap;import io.netty.channel.ChannelFuture;import io.netty.channel.ChannelHandlerContext;import io.netty.channel.ChannelInitializer;import io.netty.channel.ChannelPipeline;import io.netty.channel.EventLoopGroup;import io.netty.channel.SimpleChannelInboundHandler;import io.netty.channel.nio.NioEventLoopGroup;import io.netty.channel.socket.SocketChannel;import io.netty.channel.socket.nio.NioServerSocketChannel;import io.netty.handler.codec.LengthFieldBasedFrameDecoder;import io.netty.handler.codec.LengthFieldPrepender;import io.netty.handler.codec.string.StringDecoder;import io.netty.handler.codec.string.StringEncoder;import io.netty.util.CharsetUtil;public class NettyServer {private static final String IP = "127.0.0.1";private static final int PORT = 9991;/** 用于分配处理业务线程的线程组个数 */protected static final int BIZGROUPSIZE = Runtime.getRuntime().availableProcessors() * 2; // 默认/** 业务出现线程大小 */protected static final int BIZTHREADSIZE = 4;/* * NioEventLoopGroup实际上就是个线程池, * NioEventLoopGroup在后台启动了n个NioEventLoop来处理Channel事件, * 每一个NioEventLoop负责处理m个Channel, * NioEventLoopGroup从NioEventLoop数组里挨个取出NioEventLoop来处理Channel */private static final EventLoopGroup bossGroup = new NioEventLoopGroup(BIZGROUPSIZE);private static final EventLoopGroup workerGroup = new NioEventLoopGroup(BIZTHREADSIZE);protected static void start() throws Exception {try {ServerBootstrap b = new ServerBootstrap();b.group(bossGroup, workerGroup);b.channel(NioServerSocketChannel.class);b.childHandler(new ChannelInitializer<SocketChannel>() {@Overridepublic void initChannel(SocketChannel ch) throws Exception {ChannelPipeline pipeline = ch.pipeline();pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));pipeline.addLast("frameEncoder", new LengthFieldPrepender(4));pipeline.addLast("decoder", new StringDecoder(CharsetUtil.UTF_8));pipeline.addLast("encoder", new StringEncoder(CharsetUtil.UTF_8));pipeline.addLast(new TcpServerHandler());}});ChannelFuture channelFuture = b.bind(IP, PORT).sync();channelFuture.channel().closeFuture().sync();System.out.println("server is started");} catch (Exception e) {e.printStackTrace();} finally {workerGroup.shutdownGracefully().sync();bossGroup.shutdownGracefully().sync();}}public static void main(String[] args) throws Exception {start();}private static class TcpServerHandler extends SimpleChannelInboundHandler<Object> {@Overrideprotected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {System.out.println("SERVER receive msg:" + msg);if (msg != null && msg.toString().trim().length() > 0) {ctx.channel().writeAndFlush("server accepted " + msg);}}@Overridepublic void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {cause.printStackTrace();ctx.close();}}}
package com.netty;import io.netty.bootstrap.Bootstrap;import io.netty.channel.Channel;import io.netty.channel.ChannelFuture;import io.netty.channel.ChannelHandlerContext;import io.netty.channel.ChannelInitializer;import io.netty.channel.ChannelOption;import io.netty.channel.ChannelPipeline;import io.netty.channel.EventLoopGroup;import io.netty.channel.SimpleChannelInboundHandler;import io.netty.channel.nio.NioEventLoopGroup;import io.netty.channel.socket.nio.NioSocketChannel;import io.netty.handler.codec.LengthFieldBasedFrameDecoder;import io.netty.handler.codec.LengthFieldPrepender;import io.netty.handler.codec.string.StringDecoder;import io.netty.handler.codec.string.StringEncoder;import io.netty.util.CharsetUtil;public class TcpClient {public static String HOST = "127.0.0.1";public static int PORT = 9991;private static EventLoopGroup group = new NioEventLoopGroup();public static void main(String[] args) throws Exception {try {Bootstrap b = new Bootstrap();b.group(group).channel(NioSocketChannel.class);b.handler(new ChannelInitializer<Channel>() {@Overrideprotected void initChannel(Channel ch) throws Exception {ChannelPipeline pipeline = ch.pipeline();pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));pipeline.addLast("frameEncoder", new LengthFieldPrepender(4));pipeline.addLast("decoder", new StringDecoder(CharsetUtil.UTF_8));pipeline.addLast("encoder", new StringEncoder(CharsetUtil.UTF_8));pipeline.addLast("handler", new TcpServerHandler());}});b.option(ChannelOption.SO_KEEPALIVE, true);// 连接服务端ChannelFuture f = b.connect(HOST, PORT).sync();f.channel().closeFuture().sync();} catch (Exception e) {e.printStackTrace();}}private static class TcpServerHandler extends SimpleChannelInboundHandler<Object> {@Overridepublic void channelActive(ChannelHandlerContext ctx) throws Exception {ctx.writeAndFlush("hello");}@Overrideprotected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {System.out.println("client receive msg:" + msg);if (msg != null && msg.toString().trim().length() > 0) {ctx.channel().writeAndFlush("client send " + msg);}}@Overridepublic void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {System.out.println("Unexpected exception from downstream." + cause);ctx.close();}}}

                                             
0 0
原创粉丝点击