| // https://mvnrepository.com/artifact/io.netty/netty-all |
| implementation group: 'io.netty', name: 'netty-all', version: '4.1.51.Final' |
| public class NettyServer { |
| public static void main(String[] args) throws Exception { |
| |
| |
| |
| |
| |
| |
| |
| EventLoopGroup bossGroup = new NioEventLoopGroup(); |
| EventLoopGroup workerGroup = new NioEventLoopGroup(); |
| |
| try { |
| |
| ServerBootstrap bootstrap = new ServerBootstrap(); |
| |
| |
| bootstrap.group(bossGroup, workerGroup) |
| .channel(NioServerSocketChannel.class) |
| .option(ChannelOption.SO_BACKLOG, 128) |
| .childOption(ChannelOption.SO_KEEPALIVE, true) |
| |
| .childHandler(new ChannelInitializer<SocketChannel>() { |
| |
| @Override |
| protected void initChannel(SocketChannel ch) throws Exception { |
| |
| ch.pipeline().addLast(new NettyServerHandler()); |
| } |
| }); |
| |
| System.out.println(".....服务器 is ready..."); |
| |
| |
| |
| ChannelFuture cf = bootstrap.bind(6668).sync(); |
| |
| |
| cf.channel().closeFuture().sync(); |
| }finally { |
| bossGroup.shutdownGracefully(); |
| workerGroup.shutdownGracefully(); |
| } |
| |
| } |
| |
| } |
| public class NettyServerHandler extends ChannelInboundHandlerAdapter { |
| |
| |
| |
| |
| |
| |
| @Override |
| public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { |
| System.out.println("server ctx =" + ctx); |
| |
| |
| ByteBuf buf = (ByteBuf) msg; |
| System.out.println("客户端发送消息是:" + buf.toString(CharsetUtil.UTF_8)); |
| System.out.println("客户端地址:" + ctx.channel().remoteAddress()); |
| } |
| |
| |
| @Override |
| public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { |
| |
| |
| |
| |
| ctx.writeAndFlush(Unpooled.copiedBuffer("hello, 客户端~(>^ω^<)喵1", CharsetUtil.UTF_8)); |
| } |
| |
| |
| @Override |
| public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { |
| ctx.close(); |
| } |
| } |
| public class NettyClient { |
| public static void main(String[] args) throws Exception { |
| |
| |
| EventLoopGroup group = new NioEventLoopGroup(); |
| |
| try { |
| |
| |
| Bootstrap bootstrap = new Bootstrap(); |
| |
| |
| bootstrap.group(group) |
| .channel(NioSocketChannel.class) |
| .handler(new ChannelInitializer<SocketChannel>() { |
| @Override |
| protected void initChannel(SocketChannel ch) throws Exception { |
| ch.pipeline().addLast(new NettyClientHandler()); |
| } |
| }); |
| |
| System.out.println("客户端 ok.."); |
| |
| |
| |
| ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 6668).sync(); |
| |
| channelFuture.channel().closeFuture().sync(); |
| }finally { |
| group.shutdownGracefully(); |
| } |
| } |
| } |
| public class NettyClientHandler extends ChannelInboundHandlerAdapter { |
| |
| |
| @Override |
| public void channelActive(ChannelHandlerContext ctx) throws Exception { |
| System.out.println("client " + ctx); |
| ctx.writeAndFlush(Unpooled.copiedBuffer("hello, server: (>^ω^<)喵", CharsetUtil.UTF_8)); |
| } |
| |
| |
| @Override |
| public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { |
| ByteBuf buf = (ByteBuf) msg; |
| System.out.println("服务器回复的消息:" + buf.toString(CharsetUtil.UTF_8)); |
| System.out.println("服务器的地址: "+ ctx.channel().remoteAddress()); |
| } |
| |
| @Override |
| public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { |
| cause.printStackTrace(); |
| ctx.close(); |
| } |
| } |
| .....服务器 is ready... |
| server ctx =ChannelHandlerContext(NettyServerHandler |
| 客户端发送消息是:hello, server: (>^ω^<)喵 |
| 客户端地址:/127.0.0.1:55542 |
| |
| 客户端 ok.. |
| client ChannelHandlerContext(NettyClientHandler |
| 服务器回复的消息:hello, 客户端~(>^ω^<)喵1 |
| 服务器的地址: /127.0.0.1:6668 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
2021-08-08 Sentinel限流、降级配置详解
2021-08-08 导入项目后报错问题
2021-08-08 Redis入门(三):新数据类型