Netty入门实战
-
新建工程
-
添加maven依赖
https://mvnrepository.com
<!-- https://mvnrepository.com/artifact/io.netty/netty-all --> <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.51.Final</version> </dependency>
-
创建客户端类
package cn.com.yundong.netty; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; import io.netty.util.CharsetUtil; /** * @ClassName NettyDemoClientHandler * @Description TODO * @Author LXW * @Date 2020-07-11 10:12 **/ public class NettyDemoClientHandler extends SimpleChannelInboundHandler<ByteBuf>{ @Override protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) throws Exception { System.out.println("接收到的消息: " + byteBuf.toString(CharsetUtil.UTF_8)); } }
-
创建客户端启动类
package cn.com.yundong.netty; import io.netty.bootstrap.Bootstrap; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelInitializer; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.util.CharsetUtil; import java.net.InetSocketAddress; /** * @ClassName NettyDemoClientBootStrap * @Description TODO * @Author LXW * @Date 2020-07-11 10:26 **/ public class NettyDemoClientBootStrap { private final String host; private final Integer port; public NettyDemoClientBootStrap(String host, Integer port) { this.host = host; this.port = port; } public void run() throws InterruptedException { // 线程池 EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap bs = new Bootstrap(); bs.group(group) .channel(NioSocketChannel.class) .remoteAddress(new InetSocketAddress(host, port)) .handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel socketChannel) throws Exception { socketChannel.pipeline().addLast(new NettyDemoClientHandler()); } }); // 连接到远程节点,等待连接完成 ChannelFuture future = bs.connect().sync(); // 发送消息到服务器端 future.channel().writeAndFlush(Unpooled.copiedBuffer("Hello World", CharsetUtil.UTF_8)); // 阻塞操作 future.channel().closeFuture().sync(); } finally { group.shutdownGracefully().sync(); } } public static void main(String[] args) throws InterruptedException { new NettyDemoClientBootStrap("127.0.0.1", 18080).run(); } }
-
创建服务器端类
package cn.com.yundong.netty; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.util.CharsetUtil; /** * @ClassName NettyDemoServerHandler * @Description TODO * @Author LXW * @Date 2020-07-11 11:11 **/ public class NettyDemoServerHandler extends ChannelInboundHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { ByteBuf in = (ByteBuf) msg; System.out.println("收到客户端发过来的消息: " + in.toString(CharsetUtil.UTF_8)); // 写入并发送到客户端 ctx.writeAndFlush(Unpooled.copiedBuffer("你好,我是服务端,我已经收到你发送的消息", CharsetUtil.UTF_8)); } }
-
创建服务器端启动类
package cn.com.yundong.netty; import io.netty.bootstrap.Bootstrap; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelInitializer; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.util.CharsetUtil; import java.net.InetSocketAddress; /** * @ClassName NettyDemoClientBootStrap * @Description TODO * @Author LXW * @Date 2020-07-11 10:26 **/ public class NettyDemoClientBootStrap { private final String host; private final Integer port; public NettyDemoClientBootStrap(String host, Integer port) { this.host = host; this.port = port; } public void run() throws InterruptedException { // 线程池 EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap bs = new Bootstrap(); bs.group(group) .channel(NioSocketChannel.class) .remoteAddress(new InetSocketAddress(host, port)) .handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel socketChannel) throws Exception { socketChannel.pipeline().addLast(new NettyDemoClientHandler()); } }); // 连接到远程节点,等待连接完成 ChannelFuture future = bs.connect().sync(); // 发送消息到服务器端 future.channel().writeAndFlush(Unpooled.copiedBuffer("Hello World", CharsetUtil.UTF_8)); // 阻塞操作 future.channel().closeFuture().sync(); } finally { group.shutdownGracefully().sync(); } } public static void main(String[] args) throws InterruptedException { new NettyDemoClientBootStrap("127.0.0.1", 18080).run(); } }
-
执行结果
服务端
客户端
-
参考资料
- https://www.cnblogs.com/chamu/p/13217490.html