netty3
1. 服务端 1服务2线程池2工厂
代码在csdn下载
public static void main(String[] args) { //1. 服务类 ServerBootstrap bootstrap = new ServerBootstrap(); //2. boss线程监听端口,worker线程负责数据读写 ExecutorService boss = Executors.newCachedThreadPool(); ExecutorService worker = Executors.newCachedThreadPool(); //3. 设置niosocket工厂 即为ServerBootstrap 指定线程池 bootstrap.setFactory(new NioServerSocketChannelFactory(boss, worker)); //4. 设置管道的工厂 bootstrap.setPipelineFactory(new ChannelPipelineFactory() { @Override public ChannelPipeline getPipeline() throws Exception { ChannelPipeline pipeline = Channels.pipeline(); pipeline.addLast("decoder", new StringDecoder()); //类似于过滤器 pipeline.addLast("encoder", new StringEncoder()); pipeline.addLast("helloHandler", new HelloHandler()); return pipeline; } }); bootstrap.bind(new InetSocketAddress(11101)); System.out.println("Server start!!!"); }
服务端:个人还是比较喜欢netty3的风格的
1服务2线程池 2工厂
//1. 服务类 ClientBootstrap bootstrap = new ClientBootstrap(); //2. 线程池 ExecutorService boss = Executors.newCachedThreadPool(); ExecutorService worker = Executors.newCachedThreadPool(); //3. socket工厂 bootstrap.setFactory(new NioClientSocketChannelFactory(boss, worker)); //4. 管道工厂 bootstrap.setPipelineFactory(new ChannelPipelineFactory() { @Override public ChannelPipeline getPipeline() throws Exception { ChannelPipeline pipeline = Channels.pipeline(); pipeline.addLast("decoder", new StringDecoder()); pipeline.addLast("encoder", new StringEncoder()); pipeline.addLast("hiHandler", new HiHandler()); return pipeline; } }); //5.连接服务端 ChannelFuture connect = bootstrap.connect(new InetSocketAddress("127.0.0.1", 11101)); Channel channel = connect.getChannel(); System.out.println("client start"); Scanner scanner = new Scanner(System.in); while(true){ System.out.println("请输入"); channel.write(scanner.next()); }
Netty4
服务端
//1 初始化用于Acceptor的主线程池以及拥有I/O工作的从线程 EventLoopGroup boss = new NioEventLoopGroup(); EventLoopGroup worker = new NioEventLoopGroup(); try { //2 初始化ServerBootstrap实例,netty服务器开发的入口处 ServerBootstrap serverBootstrap = new ServerBootstrap(); //3 绑定 主从线程池 serverBootstrap.group(boss,worker) //4 设置通道Channel的类型 .channel(NioServerSocketChannel.class) //5 设置ServerSocketChannel的处理器 .handler(new LoggingHandler()) // 6设置子通道也就是SocketChannel的处理器 .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel socketChannel) throws Exception { socketChannel.pipeline().addLast(new ServerHandler()); socketChannel.pipeline().addLast("decoder", new StringDecoder()); socketChannel.pipeline().addLast("encoder", new StringEncoder()); } }) // 7 设置ServerSocketChannel的选项 .option(ChannelOption.SO_BACKLOG,128) // 8 设置子通道也就是SocketChannel的选项 .childOption(ChannelOption.SO_KEEPALIVE,true); //9 绑定监听端口 ChannelFuture channelFuture = serverBootstrap.bind("127.0.0.1",26005).sync(); channelFuture.channel().closeFuture().sync(); } finally { worker.shutdownGracefully(); boss.shutdownGracefully(); }
客户端
//1 初始化用于连接及IO工作的线程池 EventLoopGroup workGroup = new NioEventLoopGroup(); try { //2 初始化Bootstrap,Bootstrap是netty客户端应用开发的入口 Bootstrap bootstrap = new Bootstrap(); //3 绑定线程池与Bootstrap的关系 bootstrap.group(workGroup); //4 指定通道channel的类型由于是客户端,故而使用NioSocketChannel bootstrap.channel(NioSocketChannel.class); //5 设置SocketChannel的选项 bootstrap.option(ChannelOption.SO_KEEPALIVE,true); //6 设置SocketChannel的处理器,其内部就是真是的业务逻辑 bootstrap.handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel socketChannel) throws Exception { socketChannel.pipeline().addLast(new ClientHandler()); socketChannel.pipeline().addLast("decoder", new StringDecoder()); socketChannel.pipeline().addLast("encoder", new StringEncoder()); } }); // 7.start the client 指定服务器地址 ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 26005).sync(); Channel channel = channelFuture.channel(); System.out.println("client start"); Scanner scanner = new Scanner(System.in); while (true){ System.out.println("请输入客户端发送内容:"); channel.write(scanner.next()); channel.flush(); } // 等待直到连接关闭 // channelFuture.channel().closeFuture().sync(); } finally { workGroup.shutdownGracefully(); }