【问题标题】:Not able to send an object with netty无法使用 netty 发送对象
【发布时间】:2013-09-10 13:55:24
【问题描述】:

我使用了一些示例来发送和接收字符串,它们运行良好。问题是我试图调整代码(阅读 api 和示例)以发送可序列化对象,但它们不起作用。我没有收到任何错误消息,永远不会调用通道读取方法,因此服务器永远不会收到消息。我读到它可能与某种分隔符有关,但我找不到任何线索

这是代码,我不会包括添加和删除的处理程序,因为它们可以工作

服务器初始化程序

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());
}

频道读取

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

}

运行服务器

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();

    }

这里是客户端

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());

}

和主要的(服务器说客户端已连接)

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();
                    }

最后,没有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);       
}

非常感谢您的宝贵时间

【问题讨论】:

    标签: java network-programming netty


    【解决方案1】:

    检查从写入操作返回的 ChannelFuture。我打赌它失败了。

    【讨论】:

    • 您说得对,先生,它确实失败了。你对它为什么会失败有任何线索吗?我认为我以正确的方式编写它,原因方法正在抛出 null
    • 如果失败,应该有一个 Throwable。你能显示堆栈跟踪吗?
    • 它不会抛出任何东西,或者我不知道抓住它的正确方法。我试图捕捉任何异常,它不会捕捉到任何东西。我还要求 isDone() 并且它说 false 所以也许这就是我没有得到任何异常的原因。提前致谢
    • 后来我写了一个循环while(!future.isDone())(future就是write返回的channelfuture,一直没有出来。
    • 现在我明白了……您需要调用 channel.writeAndFlush(..) 才能将其实际刷新到套接字中。请参阅 apidocs ;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-12
    • 1970-01-01
    • 1970-01-01
    • 2014-08-08
    相关资源
    最近更新 更多