diff --git a/netty/service/src/main/java/cn/bunny/service/netty/PackageClient.java b/netty/service/src/main/java/cn/bunny/service/netty/PackageClient.java new file mode 100644 index 0000000..b7c762c --- /dev/null +++ b/netty/service/src/main/java/cn/bunny/service/netty/PackageClient.java @@ -0,0 +1,47 @@ +package cn.bunny.service.netty; + +import io.netty.bootstrap.Bootstrap; +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.SocketChannel; +import io.netty.channel.socket.nio.NioSocketChannel; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class PackageClient { + public static void main(String[] args) { + NioEventLoopGroup worker = new NioEventLoopGroup(); + try { + Bootstrap bootstrap = new Bootstrap(); + bootstrap.channel(NioSocketChannel.class); + bootstrap.group(worker); + bootstrap.handler(new ChannelInitializer() { + @Override + protected void initChannel(SocketChannel ch) throws Exception { + ch.pipeline().addLast(new ChannelInboundHandlerAdapter() { + @Override + public void channelActive(ChannelHandlerContext ctx) throws Exception { + log.debug("sending..."); + for (int i = 0; i < 10; i++) { + ByteBuf buffer = ctx.alloc().buffer(); + buffer.writeBytes(new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}); + ctx.writeAndFlush(buffer); + } + } + }); + } + }); + ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 8080).sync(); + channelFuture.channel().closeFuture().sync(); + + } catch (InterruptedException e) { + log.error("client error", e); + } finally { + worker.shutdownGracefully(); + } + } +} diff --git a/netty/service/src/main/java/cn/bunny/service/netty/PackageServer.java b/netty/service/src/main/java/cn/bunny/service/netty/PackageServer.java new file mode 100644 index 0000000..c79badf --- /dev/null +++ b/netty/service/src/main/java/cn/bunny/service/netty/PackageServer.java @@ -0,0 +1,52 @@ +package cn.bunny.service.netty; + +import io.netty.bootstrap.ServerBootstrap; +import io.netty.channel.*; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.SocketChannel; +import io.netty.channel.socket.nio.NioServerSocketChannel; +import io.netty.handler.logging.LogLevel; +import io.netty.handler.logging.LoggingHandler; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class PackageServer { + public static void main(String[] args) throws InterruptedException { + NioEventLoopGroup boss = new NioEventLoopGroup(1); + NioEventLoopGroup work = new NioEventLoopGroup(); + + try { + ServerBootstrap serverBootstrap = new ServerBootstrap(); + serverBootstrap.channel(NioServerSocketChannel.class); + serverBootstrap.group(boss, work); + // 接受缓冲区 + serverBootstrap.option(ChannelOption.SO_RCVBUF, 10); + serverBootstrap.childHandler(new ChannelInitializer() { + @Override + protected void initChannel(SocketChannel socketChannel) throws Exception { + socketChannel.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG)); + socketChannel.pipeline().addLast(new ChannelInboundHandlerAdapter() { + @Override + public void channelActive(ChannelHandlerContext ctx) throws Exception { + log.debug("connected {}", ctx.channel()); + super.channelActive(ctx); + } + + @Override + public void channelInactive(ChannelHandlerContext ctx) throws Exception { + log.debug("disconnect {}", ctx.channel()); + super.channelInactive(ctx); + } + }); + } + }); + ChannelFuture channelFuture = serverBootstrap.bind(8080).sync(); + channelFuture.channel().closeFuture().sync(); + } catch (Exception exception) { + log.error("服务器错误。。。", exception); + } finally { + boss.shutdownGracefully(); + work.shutdownGracefully(); + } + } +}