Netty教程

Netty教程本篇文章总结下 Netty 喜欢的朋友点赞支持下哈

本篇文章总结下Netty,喜欢的朋友点赞支持下哈。

一,Netty入门案例

@ChannelHandler.Sharable public class EchoClientHandler extends SimpleChannelInboundHandler<ByteBuf> { 
    @Override public void channelActive(ChannelHandlerContext ctx) { 
    ctx.writeAndFlush(Unpooled.copiedBuffer("Hello, Netty!", CharsetUtil.UTF_8)); } @Override protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf in) { 
    System.out.println("Client received: " + in.toString(CharsetUtil.UTF_8)); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { 
    cause.printStackTrace(); ctx.close(); } } 
public class EchoClient { 
    private final String host; private final int port; public EchoClient(String host, int port){ 
    this.host = host; this.port = port; } public void start() throws Exception { 
    EventLoopGroup group = new NioEventLoopGroup(); try { 
    Bootstrap b = new Bootstrap(); b.group(group) .channel(NioSocketChannel.class) .remoteAddress(new InetSocketAddress(host, port)) .handler(new ChannelInitializer<SocketChannel>() { 
    @Override protected void initChannel(SocketChannel socketChannel) { 
    socketChannel.pipeline().addLast(new EchoClientHandler()); } }); ChannelFuture f = b.connect().sync(); f.channel().closeFuture().sync(); }finally { 
    group.shutdownGracefully().sync(); } } public static void main(String[] args) throws Exception { 
    /*if(args.length != 2){ System.err.println("Usage: " + EchoClient.class.getSimpleName() + " 
    
    
      "); return; }*/ 
     
    String host = "127.0.0.1"; int port = Integer.parseInt("8089"); new EchoClient(host, port).start(); } } 

服务器:

@ChannelHandler.Sharable public class EchoServerHandler extends ChannelInboundHandlerAdapter { 
    @Override public void channelRead(ChannelHandlerContext ctx, Object msg){ 
    ByteBuf in = (ByteBuf) msg; System.out.println("Server received: " + in.toString(CharsetUtil.UTF_8)); ctx.write(in); } @Override public void channelReadComplete(ChannelHandlerContext ctx){ 
    ctx.writeAndFlush(Unpooled.EMPTY_BUFFER) .addListener(ChannelFutureListener.CLOSE); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { 
    cause.printStackTrace(); ctx.close(); } } 
public class EchoServer { 
    private final int port; public EchoServer(int port){ 
    this.port = port; } public void start() throws Exception { 
    final EchoServerHandler serverHandler = new EchoServerHandler(); EventLoopGroup group = new NioEventLoopGroup(); try { 
    ServerBootstrap b = new ServerBootstrap(); b.group(group) .channel(NioServerSocketChannel.class) .localAddress(new InetSocketAddress(port)) .childHandler(new ChannelInitializer<SocketChannel>() { 
    @Override protected void initChannel(SocketChannel socketChannel) { 
    socketChannel.pipeline().addLast(serverHandler); } }); ChannelFuture f = b.bind().sync(); f.channel().closeFuture().sync(); }finally { 
    group.shutdownGracefully().sync(); } } public static void main(String[] args) throws Exception { 
    /*if(args.length != 1){ System.err.println("Usage :" + EchoServer.class.getSimpleName() + " 
   
     "); return; }*/ 
    int port = Integer.parseInt("8089"); new EchoServer(port).start(); } } 

···

二,Netty核心组件

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/204111.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月19日 下午8:58
下一篇 2026年3月19日 下午8:59


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号