From 1b6a72eddc1a194e39234bcf2229c96858f20c61 Mon Sep 17 00:00:00 2001 From: bingor Date: Thu, 27 Oct 2022 17:53:44 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BD=91=E7=BB=9C=E7=BC=96=E7=A8=8B=E5=9F=BA?= =?UTF-8?q?=E7=A1=80-netty?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/msb/netty/Client.java | 80 ++++++++++++++++++++++++ src/main/java/com/msb/netty/Server.java | 82 +++++++++++++++++++++++++ 2 files changed, 162 insertions(+) create mode 100644 src/main/java/com/msb/netty/Client.java create mode 100644 src/main/java/com/msb/netty/Server.java diff --git a/src/main/java/com/msb/netty/Client.java b/src/main/java/com/msb/netty/Client.java new file mode 100644 index 0000000..f24c098 --- /dev/null +++ b/src/main/java/com/msb/netty/Client.java @@ -0,0 +1,80 @@ +package com.msb.netty;/** + * @Author bingor + * @Date 2022/10/27 17:33 + * @Description: com.msb.netty + * @Version: 1.0 + */ + +import io.netty.bootstrap.Bootstrap; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import io.netty.channel.*; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.SocketChannel; +import io.netty.channel.socket.nio.NioSocketChannel; +import io.netty.util.ReferenceCountUtil; +import io.netty.util.ReferenceCounted; + +/** + *@ClassName Client + *@Description TODO + *@Author bingor + *@Date 2022/10/27 17:33 + *@Version 3.0 + */ +public class Client { + + public void clientStart() { + EventLoopGroup workerGroup = new NioEventLoopGroup(); + Bootstrap bootstrap = new Bootstrap(); + bootstrap.group(workerGroup) + .channel(NioSocketChannel.class) + .handler(new ChannelInitializer() { + @Override + protected void initChannel(SocketChannel socketChannel) throws Exception { + System.out.println("client initialized"); + socketChannel.pipeline().addLast(new ClientHandler()); + } + }); + + System.out.println("start to connect...."); + try { + ChannelFuture future = bootstrap.connect("127.0.0.1", 8888).sync(); + future.channel().closeFuture().sync(); + } catch (InterruptedException e) { + e.printStackTrace(); + } finally { + workerGroup.shutdownGracefully(); + } + } + + public static void main(String[] args) { + new Client().clientStart(); + } +} + +class ClientHandler extends ChannelInboundHandlerAdapter { + @Override + public void channelActive(ChannelHandlerContext ctx) throws Exception { +// super.channelActive(ctx); + System.out.println("channel is activated."); + final ChannelFuture future = ctx.writeAndFlush(Unpooled.copiedBuffer("hello netty server".getBytes())); + future.addListener(new ChannelFutureListener() { + @Override + public void operationComplete(ChannelFuture channelFuture) throws Exception { + System.out.println("msg send"); + } + }); + } + + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { +// super.channelRead(ctx, msg); + try { + ByteBuf buf = (ByteBuf) msg; + System.out.println(buf.toString()); + } finally { + ReferenceCountUtil.release(msg); + } + } +} diff --git a/src/main/java/com/msb/netty/Server.java b/src/main/java/com/msb/netty/Server.java new file mode 100644 index 0000000..00ab6c3 --- /dev/null +++ b/src/main/java/com/msb/netty/Server.java @@ -0,0 +1,82 @@ +package com.msb.netty;/** + * @Author bingor + * @Date 2022/10/27 16:47 + * @Description: com.msb.netty + * @Version: 1.0 + */ + +import io.netty.bootstrap.ServerBootstrap; +import io.netty.buffer.ByteBuf; +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.channel.socket.nio.NioSocketChannel; +import io.netty.util.CharsetUtil; + +import java.nio.charset.Charset; + +/** + *@ClassName Server + *@Description TODO + *@Author bingor + *@Date 2022/10/27 16:47 + *@Version 3.0 + */ +public class Server { + + private int port; + public Server(int port) { + this.port = port; + } + + public void serverStart() { + EventLoopGroup bossGroup = new NioEventLoopGroup(); + EventLoopGroup workerGroup = new NioEventLoopGroup(); + ServerBootstrap bootstrap = new ServerBootstrap(); + bootstrap.group(bossGroup, workerGroup) + .channel(NioServerSocketChannel.class) + .childHandler(new ChannelInitializer() { + @Override + protected void initChannel(SocketChannel socketChannel) throws Exception { + socketChannel.pipeline().addLast(new Handler()); + } + }); + + try { + ChannelFuture future = bootstrap.bind(this.port).sync(); + future.channel().closeFuture().sync(); + } catch (InterruptedException e) { + e.printStackTrace(); + } finally { + workerGroup.shutdownGracefully(); + bossGroup.shutdownGracefully(); + } + + } + + public static void main(String[] args) { + new Server(8888).serverStart(); + } + +} + +class Handler extends ChannelInboundHandlerAdapter { + + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { +// super.channelRead(ctx, msg); + System.out.println("server: channel read"); + ByteBuf buf = (ByteBuf) msg; + System.out.println(new String(buf.toString(CharsetUtil.UTF_8))); + ctx.writeAndFlush(msg); + ctx.close(); + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { +// super.exceptionCaught(ctx, cause); + cause.printStackTrace(); + ctx.close(); + } +} \ No newline at end of file