netty(二)——对象传递

本文介绍了如何使用Netty框架在服务器和客户端之间传递实现了Serializable接口的自定义对象,包括服务器端代码、客户端代码及核心流程解析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上一篇中服务端把字符串转换成字符数组后传给客户端,那么能否直接传递对象呢?答案当然是可以,不过这个对象必须实现了Serializable接口(如果对象没实现Serializable接口,那么将不能传递该对象,但是程序也没有报以错,具体还得查看netty源码怎么写的)。

要传递的对象:

public class User implements Serializable {

    private Long id;
    private String username;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                '}';
    }
}

 

服务器端代码:

public class ObjectServer {

    public void run() throws InterruptedException {
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ch.pipeline().addLast(new ObjectDecoder(ClassResolvers.cacheDisabled(this.getClass().getClassLoader())),new ObjectEncoder(), new ObjectServerHandler());
                        }
                    })
                    .option(ChannelOption.SO_BACKLOG, 128)
                    .childOption(ChannelOption.SO_KEEPALIVE, true);
            ChannelFuture f = b.bind(7777).sync();
            System.out.println("server started.");
            f.channel().closeFuture().sync();
        } finally {
            workerGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
        }
    }

    public static void main(String[] args) throws Exception {
        new ObjectServer().run();
    }
}

public class ObjectServerHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        User user = (User) msg;
        System.out.println(user);
        user.setUsername("ooxx");
        ctx.write(user);
        ctx.flush();
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.close();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        //打印异常信息并关闭连接
        cause.printStackTrace();
        ctx.close();
    }
}

 

客户端代码:

public class ObjectClient {

    public static void main(String[] args) throws InterruptedException {
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            Bootstrap b = new Bootstrap();
            b.group(workerGroup)
                    .channel(NioSocketChannel.class)
                    .option(ChannelOption.SO_KEEPALIVE, true)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ch.pipeline().addLast(new ObjectDecoder(ClassResolvers.cacheDisabled(this.getClass().getClassLoader())),new ObjectEncoder(), new ObjectClientHandler());
                        }
                    });
            ChannelFuture f = b.connect("127.0.0.1", 7777).sync();
            f.channel().closeFuture().sync();
        } finally {
            workerGroup.shutdownGracefully();
        }
    }
}

public class ObjectClientHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        User user = new User();
        user.setId(1L);
        user.setUsername("netty");
        ctx.write(user);
        ctx.flush();
        System.out.println("xxx");
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        User user = (User) msg;
        System.out.println(user);
        ctx.close();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}

 

原文:http://www.nexusy.com/2014/06/11/netty-2/

### Netty 入门教程 Netty 是一款基于 Java 的高性能网络应用框架,它简化了 TCP 和 UDP 协议的开发过程。以下是关于 Netty 基础知识和入门使用的详细介绍。 #### 1. Netty 概述 Netty 提供了一种异步事件驱动的方式来处理网络通信,使得开发者可以专注于业务逻辑而无需过多关注底层细节[^2]。相比于直接使用 JDK 的 NIO 类库,Netty 封装了许多复杂的实现细节,从而降低了开发难度并提高了效率[^3]。 #### 2. Netty 的核心组件 - **Channel**: 表示一个连接到实体(例如套接字)的对象,用于执行 I/O 操作。 - **EventLoop**: 负责监听 Channel 上发生的事件,并将其传递给相应的处理器。 - **ChannelPipeline & ChannelHandler**: 组件链表结构,负责拦截入站和出站的数据流,允许自定义行为来处理请求或响应。 这些概念构成了 Netty 架构的核心部分[^1]。 #### 3. 创建一个简单的 HTTP Server 为了更好地理解如何实际运用 Netty 来构建项目,这里提供了一个基本的例子——创建一个支持 GET 请求的小型 Web 服务器: ```java import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel; public class SimpleHttpServer { public static void main(String[] args) throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(); // (1) EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); // (2) b.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) // (3) .childHandler(new HttpInitializer()); // (4) ChannelFuture f = b.bind(8080).sync(); // (5) System.out.println("HTTP server started at http://localhost:8080/"); f.channel().closeFuture().sync(); // (6) } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } } } ``` 上述代码展示了如何初始化 Netty 并绑定端口以等待客户端连接。 #### 4. 解决常见问题 - 粘包/拆包现象 当涉及到进制协议或者大文件输时,可能会遇到数据帧被分割成多个片段的情况。为此,Netty 提供了几种内置解码器解决方案,如 `LineBasedFrameDecoder`、`DelimiterBasedFrameDecoder` 和 `FixedLengthFrameDecoder` 等[^5]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值