简体   繁体   English

无法发送带有净值的对象

[英]Not able to send an object with netty

I've used some examples to send and receive strings and they have worked perfectly. 我使用了一些示例来发送和接收字符串,并且它们运行良好。 The thing is I have tried to adapt the code(reading the api and examples) to send serializable objects and they dont work. 问题是我试图修改代码(阅读api和示例)以发送可序列化的对象,但它们不起作用。 I don't get any error message, channel read method is never invoked so server never gets the message. 我没有收到任何错误消息,通道读取方法从不调用,因此服务器从不接收消息。 I read it could be related to some kind of delimeter, but I can't find any clue regarding it 我读到它可能与某种距离有关,但我找不到任何线索

Here is the code, i won't include handler added and removed because they work 这是代码,我不会添加和删除处理程序,因为它们可以正常工作

Server initiaizer 服务器初始化器

protected void initChannel(SocketChannel arg0) throws Exception {
    ChannelPipeline pipeline = arg0.pipeline();
    pipeline.addLast("decoder", new ObjectDecoder(null));
    pipeline.addLast("encoder", new ObjectEncoder());
    pipeline.addLast("handler", new ChatServerHandler());
}

Channel read 频道读取

public void channelRead0(ChannelHandlerContext arg0, Message message)
        throws Exception {
    Channel incoming = arg0.channel();
    System.out.println("received "+ message.getContent() + "from " + message.getSender() + "\r\n" );            

}

Run server 运行服务器

public void run() throws InterruptedException{
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();

    try{
        ServerBootstrap bootstrap = new ServerBootstrap()
        .group(bossGroup, workerGroup)
        .channel(NioServerSocketChannel.class)
        .childHandler(new ChatServerInitializer());         
        bootstrap.bind(port).sync().channel().closeFuture().sync();

    }

And here is the client 这是客户

protected void initChannel(SocketChannel arg0) throws Exception {
    ChannelPipeline pipeline = arg0.pipeline();


    pipeline.addLast("decoder", new ObjectDecoder(null));
    pipeline.addLast("encoder", new ObjectEncoder());
    pipeline.addLast("handler", new ChatClientHandler());

}

and the main (server says client connected) 和主服务器(服务器说客户端已连接)

public void run(){
    EventLoopGroup group = new NioEventLoopGroup();
    try {
            Bootstrap bootstrap = new Bootstrap()
                .group(group)
                .channel(NioSocketChannel.class)
                .handler(new ChatClientInitializer());

            Channel channel = bootstrap.connect(host,port).sync().channel();
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            Message message = new Message();
            message.setReceiver("user");
            message.setSender("user 2");
                    try {
                        message.setContent(in.readLine());

                        channel.write(message);

                        System.out.println("Sent " + System.currentTimeMillis());
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

Last, the message class without get/set 最后,没有获取/设置的消息类

public class Message implements Serializable {

private static final long serialVersionUID = 1L;
private String sender;
private String receiver;
private String content;
public Message(String sender, String receiver, String content){     
    this.setSender(sender);
    this.setReceiver(receiver);
    this.setContent(content);       
}

Thanks a lot for your time 非常感谢您的时间

Check the ChannelFuture returned from the write operation. 检查从写操作返回的ChannelFuture。 I bet it was failed. 我敢打赌它失败了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM